speakers.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * OXFW970-based speakers 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/device.h>
  8. #include <linux/firewire.h>
  9. #include <linux/firewire-constants.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <sound/control.h>
  15. #include <sound/core.h>
  16. #include <sound/initval.h>
  17. #include <sound/pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include "cmp.h"
  20. #include "fcp.h"
  21. #include "amdtp.h"
  22. #include "lib.h"
  23. #define OXFORD_FIRMWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x50000)
  24. /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
  25. #define OXFORD_HARDWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x90020)
  26. #define OXFORD_HARDWARE_ID_OXFW970 0x39443841
  27. #define OXFORD_HARDWARE_ID_OXFW971 0x39373100
  28. #define VENDOR_GRIFFIN 0x001292
  29. #define VENDOR_LACIE 0x00d04b
  30. #define SPECIFIER_1394TA 0x00a02d
  31. #define VERSION_AVC 0x010001
  32. struct device_info {
  33. const char *driver_name;
  34. const char *short_name;
  35. const char *long_name;
  36. int (*pcm_constraints)(struct snd_pcm_runtime *runtime);
  37. unsigned int mixer_channels;
  38. u8 mute_fb_id;
  39. u8 volume_fb_id;
  40. };
  41. struct fwspk {
  42. struct snd_card *card;
  43. struct fw_unit *unit;
  44. const struct device_info *device_info;
  45. struct snd_pcm_substream *pcm;
  46. struct mutex mutex;
  47. struct cmp_connection connection;
  48. struct amdtp_out_stream stream;
  49. bool mute;
  50. s16 volume[6];
  51. s16 volume_min;
  52. s16 volume_max;
  53. };
  54. MODULE_DESCRIPTION("FireWire speakers driver");
  55. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  56. MODULE_LICENSE("GPL v2");
  57. static int firewave_rate_constraint(struct snd_pcm_hw_params *params,
  58. struct snd_pcm_hw_rule *rule)
  59. {
  60. static unsigned int stereo_rates[] = { 48000, 96000 };
  61. struct snd_interval *channels =
  62. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  63. struct snd_interval *rate =
  64. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  65. /* two channels work only at 48/96 kHz */
  66. if (snd_interval_max(channels) < 6)
  67. return snd_interval_list(rate, 2, stereo_rates, 0);
  68. return 0;
  69. }
  70. static int firewave_channels_constraint(struct snd_pcm_hw_params *params,
  71. struct snd_pcm_hw_rule *rule)
  72. {
  73. static const struct snd_interval all_channels = { .min = 6, .max = 6 };
  74. struct snd_interval *rate =
  75. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  76. struct snd_interval *channels =
  77. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  78. /* 32/44.1 kHz work only with all six channels */
  79. if (snd_interval_max(rate) < 48000)
  80. return snd_interval_refine(channels, &all_channels);
  81. return 0;
  82. }
  83. static int firewave_constraints(struct snd_pcm_runtime *runtime)
  84. {
  85. static unsigned int channels_list[] = { 2, 6 };
  86. static struct snd_pcm_hw_constraint_list channels_list_constraint = {
  87. .count = 2,
  88. .list = channels_list,
  89. };
  90. int err;
  91. runtime->hw.rates = SNDRV_PCM_RATE_32000 |
  92. SNDRV_PCM_RATE_44100 |
  93. SNDRV_PCM_RATE_48000 |
  94. SNDRV_PCM_RATE_96000;
  95. runtime->hw.channels_max = 6;
  96. err = snd_pcm_hw_constraint_list(runtime, 0,
  97. SNDRV_PCM_HW_PARAM_CHANNELS,
  98. &channels_list_constraint);
  99. if (err < 0)
  100. return err;
  101. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  102. firewave_rate_constraint, NULL,
  103. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  104. if (err < 0)
  105. return err;
  106. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  107. firewave_channels_constraint, NULL,
  108. SNDRV_PCM_HW_PARAM_RATE, -1);
  109. if (err < 0)
  110. return err;
  111. return 0;
  112. }
  113. static int lacie_speakers_constraints(struct snd_pcm_runtime *runtime)
  114. {
  115. runtime->hw.rates = SNDRV_PCM_RATE_32000 |
  116. SNDRV_PCM_RATE_44100 |
  117. SNDRV_PCM_RATE_48000 |
  118. SNDRV_PCM_RATE_88200 |
  119. SNDRV_PCM_RATE_96000;
  120. return 0;
  121. }
  122. static int fwspk_open(struct snd_pcm_substream *substream)
  123. {
  124. static const struct snd_pcm_hardware hardware = {
  125. .info = SNDRV_PCM_INFO_MMAP |
  126. SNDRV_PCM_INFO_MMAP_VALID |
  127. SNDRV_PCM_INFO_BATCH |
  128. SNDRV_PCM_INFO_INTERLEAVED |
  129. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  130. .formats = AMDTP_OUT_PCM_FORMAT_BITS,
  131. .channels_min = 2,
  132. .channels_max = 2,
  133. .buffer_bytes_max = 4 * 1024 * 1024,
  134. .period_bytes_min = 1,
  135. .period_bytes_max = UINT_MAX,
  136. .periods_min = 1,
  137. .periods_max = UINT_MAX,
  138. };
  139. struct fwspk *fwspk = substream->private_data;
  140. struct snd_pcm_runtime *runtime = substream->runtime;
  141. int err;
  142. runtime->hw = hardware;
  143. err = fwspk->device_info->pcm_constraints(runtime);
  144. if (err < 0)
  145. return err;
  146. err = snd_pcm_limit_hw_rates(runtime);
  147. if (err < 0)
  148. return err;
  149. err = snd_pcm_hw_constraint_minmax(runtime,
  150. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  151. 5000, UINT_MAX);
  152. if (err < 0)
  153. return err;
  154. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  155. if (err < 0)
  156. return err;
  157. return 0;
  158. }
  159. static int fwspk_close(struct snd_pcm_substream *substream)
  160. {
  161. return 0;
  162. }
  163. static void fwspk_stop_stream(struct fwspk *fwspk)
  164. {
  165. if (amdtp_out_stream_running(&fwspk->stream)) {
  166. amdtp_out_stream_stop(&fwspk->stream);
  167. cmp_connection_break(&fwspk->connection);
  168. }
  169. }
  170. static int fwspk_set_rate(struct fwspk *fwspk, unsigned int sfc)
  171. {
  172. u8 *buf;
  173. int err;
  174. buf = kmalloc(8, GFP_KERNEL);
  175. if (!buf)
  176. return -ENOMEM;
  177. buf[0] = 0x00; /* AV/C, CONTROL */
  178. buf[1] = 0xff; /* unit */
  179. buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
  180. buf[3] = 0x00; /* plug 0 */
  181. buf[4] = 0x90; /* format: audio */
  182. buf[5] = 0x00 | sfc; /* AM824, frequency */
  183. buf[6] = 0xff; /* SYT (not used) */
  184. buf[7] = 0xff;
  185. err = fcp_avc_transaction(fwspk->unit, buf, 8, buf, 8,
  186. BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
  187. if (err < 0)
  188. goto error;
  189. if (err < 6 || buf[0] != 0x09 /* ACCEPTED */) {
  190. dev_err(&fwspk->unit->device, "failed to set sample rate\n");
  191. err = -EIO;
  192. goto error;
  193. }
  194. err = 0;
  195. error:
  196. kfree(buf);
  197. return err;
  198. }
  199. static int fwspk_hw_params(struct snd_pcm_substream *substream,
  200. struct snd_pcm_hw_params *hw_params)
  201. {
  202. struct fwspk *fwspk = substream->private_data;
  203. int err;
  204. mutex_lock(&fwspk->mutex);
  205. fwspk_stop_stream(fwspk);
  206. mutex_unlock(&fwspk->mutex);
  207. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  208. params_buffer_bytes(hw_params));
  209. if (err < 0)
  210. goto error;
  211. amdtp_out_stream_set_rate(&fwspk->stream, params_rate(hw_params));
  212. amdtp_out_stream_set_pcm(&fwspk->stream, params_channels(hw_params));
  213. amdtp_out_stream_set_pcm_format(&fwspk->stream,
  214. params_format(hw_params));
  215. err = fwspk_set_rate(fwspk, fwspk->stream.sfc);
  216. if (err < 0)
  217. goto err_buffer;
  218. return 0;
  219. err_buffer:
  220. snd_pcm_lib_free_vmalloc_buffer(substream);
  221. error:
  222. return err;
  223. }
  224. static int fwspk_hw_free(struct snd_pcm_substream *substream)
  225. {
  226. struct fwspk *fwspk = substream->private_data;
  227. mutex_lock(&fwspk->mutex);
  228. fwspk_stop_stream(fwspk);
  229. mutex_unlock(&fwspk->mutex);
  230. return snd_pcm_lib_free_vmalloc_buffer(substream);
  231. }
  232. static int fwspk_prepare(struct snd_pcm_substream *substream)
  233. {
  234. struct fwspk *fwspk = substream->private_data;
  235. int err;
  236. mutex_lock(&fwspk->mutex);
  237. if (amdtp_out_streaming_error(&fwspk->stream))
  238. fwspk_stop_stream(fwspk);
  239. if (!amdtp_out_stream_running(&fwspk->stream)) {
  240. err = cmp_connection_establish(&fwspk->connection,
  241. amdtp_out_stream_get_max_payload(&fwspk->stream));
  242. if (err < 0)
  243. goto err_mutex;
  244. err = amdtp_out_stream_start(&fwspk->stream,
  245. fwspk->connection.resources.channel,
  246. fwspk->connection.speed);
  247. if (err < 0)
  248. goto err_connection;
  249. }
  250. mutex_unlock(&fwspk->mutex);
  251. amdtp_out_stream_pcm_prepare(&fwspk->stream);
  252. return 0;
  253. err_connection:
  254. cmp_connection_break(&fwspk->connection);
  255. err_mutex:
  256. mutex_unlock(&fwspk->mutex);
  257. return err;
  258. }
  259. static int fwspk_trigger(struct snd_pcm_substream *substream, int cmd)
  260. {
  261. struct fwspk *fwspk = substream->private_data;
  262. struct snd_pcm_substream *pcm;
  263. switch (cmd) {
  264. case SNDRV_PCM_TRIGGER_START:
  265. pcm = substream;
  266. break;
  267. case SNDRV_PCM_TRIGGER_STOP:
  268. pcm = NULL;
  269. break;
  270. default:
  271. return -EINVAL;
  272. }
  273. amdtp_out_stream_pcm_trigger(&fwspk->stream, pcm);
  274. return 0;
  275. }
  276. static snd_pcm_uframes_t fwspk_pointer(struct snd_pcm_substream *substream)
  277. {
  278. struct fwspk *fwspk = substream->private_data;
  279. return amdtp_out_stream_pcm_pointer(&fwspk->stream);
  280. }
  281. static int fwspk_create_pcm(struct fwspk *fwspk)
  282. {
  283. static struct snd_pcm_ops ops = {
  284. .open = fwspk_open,
  285. .close = fwspk_close,
  286. .ioctl = snd_pcm_lib_ioctl,
  287. .hw_params = fwspk_hw_params,
  288. .hw_free = fwspk_hw_free,
  289. .prepare = fwspk_prepare,
  290. .trigger = fwspk_trigger,
  291. .pointer = fwspk_pointer,
  292. .page = snd_pcm_lib_get_vmalloc_page,
  293. .mmap = snd_pcm_lib_mmap_vmalloc,
  294. };
  295. struct snd_pcm *pcm;
  296. int err;
  297. err = snd_pcm_new(fwspk->card, "OXFW970", 0, 1, 0, &pcm);
  298. if (err < 0)
  299. return err;
  300. pcm->private_data = fwspk;
  301. strcpy(pcm->name, fwspk->device_info->short_name);
  302. fwspk->pcm = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  303. fwspk->pcm->ops = &ops;
  304. return 0;
  305. }
  306. enum control_action { CTL_READ, CTL_WRITE };
  307. enum control_attribute {
  308. CTL_MIN = 0x02,
  309. CTL_MAX = 0x03,
  310. CTL_CURRENT = 0x10,
  311. };
  312. static int fwspk_mute_command(struct fwspk *fwspk, bool *value,
  313. enum control_action action)
  314. {
  315. u8 *buf;
  316. u8 response_ok;
  317. int err;
  318. buf = kmalloc(11, GFP_KERNEL);
  319. if (!buf)
  320. return -ENOMEM;
  321. if (action == CTL_READ) {
  322. buf[0] = 0x01; /* AV/C, STATUS */
  323. response_ok = 0x0c; /* STABLE */
  324. } else {
  325. buf[0] = 0x00; /* AV/C, CONTROL */
  326. response_ok = 0x09; /* ACCEPTED */
  327. }
  328. buf[1] = 0x08; /* audio unit 0 */
  329. buf[2] = 0xb8; /* FUNCTION BLOCK */
  330. buf[3] = 0x81; /* function block type: feature */
  331. buf[4] = fwspk->device_info->mute_fb_id; /* function block ID */
  332. buf[5] = 0x10; /* control attribute: current */
  333. buf[6] = 0x02; /* selector length */
  334. buf[7] = 0x00; /* audio channel number */
  335. buf[8] = 0x01; /* control selector: mute */
  336. buf[9] = 0x01; /* control data length */
  337. if (action == CTL_READ)
  338. buf[10] = 0xff;
  339. else
  340. buf[10] = *value ? 0x70 : 0x60;
  341. err = fcp_avc_transaction(fwspk->unit, buf, 11, buf, 11, 0x3fe);
  342. if (err < 0)
  343. goto error;
  344. if (err < 11) {
  345. dev_err(&fwspk->unit->device, "short FCP response\n");
  346. err = -EIO;
  347. goto error;
  348. }
  349. if (buf[0] != response_ok) {
  350. dev_err(&fwspk->unit->device, "mute command failed\n");
  351. err = -EIO;
  352. goto error;
  353. }
  354. if (action == CTL_READ)
  355. *value = buf[10] == 0x70;
  356. err = 0;
  357. error:
  358. kfree(buf);
  359. return err;
  360. }
  361. static int fwspk_volume_command(struct fwspk *fwspk, s16 *value,
  362. unsigned int channel,
  363. enum control_attribute attribute,
  364. enum control_action action)
  365. {
  366. u8 *buf;
  367. u8 response_ok;
  368. int err;
  369. buf = kmalloc(12, GFP_KERNEL);
  370. if (!buf)
  371. return -ENOMEM;
  372. if (action == CTL_READ) {
  373. buf[0] = 0x01; /* AV/C, STATUS */
  374. response_ok = 0x0c; /* STABLE */
  375. } else {
  376. buf[0] = 0x00; /* AV/C, CONTROL */
  377. response_ok = 0x09; /* ACCEPTED */
  378. }
  379. buf[1] = 0x08; /* audio unit 0 */
  380. buf[2] = 0xb8; /* FUNCTION BLOCK */
  381. buf[3] = 0x81; /* function block type: feature */
  382. buf[4] = fwspk->device_info->volume_fb_id; /* function block ID */
  383. buf[5] = attribute; /* control attribute */
  384. buf[6] = 0x02; /* selector length */
  385. buf[7] = channel; /* audio channel number */
  386. buf[8] = 0x02; /* control selector: volume */
  387. buf[9] = 0x02; /* control data length */
  388. if (action == CTL_READ) {
  389. buf[10] = 0xff;
  390. buf[11] = 0xff;
  391. } else {
  392. buf[10] = *value >> 8;
  393. buf[11] = *value;
  394. }
  395. err = fcp_avc_transaction(fwspk->unit, buf, 12, buf, 12, 0x3fe);
  396. if (err < 0)
  397. goto error;
  398. if (err < 12) {
  399. dev_err(&fwspk->unit->device, "short FCP response\n");
  400. err = -EIO;
  401. goto error;
  402. }
  403. if (buf[0] != response_ok) {
  404. dev_err(&fwspk->unit->device, "volume command failed\n");
  405. err = -EIO;
  406. goto error;
  407. }
  408. if (action == CTL_READ)
  409. *value = (buf[10] << 8) | buf[11];
  410. err = 0;
  411. error:
  412. kfree(buf);
  413. return err;
  414. }
  415. static int fwspk_mute_get(struct snd_kcontrol *control,
  416. struct snd_ctl_elem_value *value)
  417. {
  418. struct fwspk *fwspk = control->private_data;
  419. value->value.integer.value[0] = !fwspk->mute;
  420. return 0;
  421. }
  422. static int fwspk_mute_put(struct snd_kcontrol *control,
  423. struct snd_ctl_elem_value *value)
  424. {
  425. struct fwspk *fwspk = control->private_data;
  426. bool mute;
  427. int err;
  428. mute = !value->value.integer.value[0];
  429. if (mute == fwspk->mute)
  430. return 0;
  431. err = fwspk_mute_command(fwspk, &mute, CTL_WRITE);
  432. if (err < 0)
  433. return err;
  434. fwspk->mute = mute;
  435. return 1;
  436. }
  437. static int fwspk_volume_info(struct snd_kcontrol *control,
  438. struct snd_ctl_elem_info *info)
  439. {
  440. struct fwspk *fwspk = control->private_data;
  441. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  442. info->count = fwspk->device_info->mixer_channels;
  443. info->value.integer.min = fwspk->volume_min;
  444. info->value.integer.max = fwspk->volume_max;
  445. return 0;
  446. }
  447. static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 };
  448. static int fwspk_volume_get(struct snd_kcontrol *control,
  449. struct snd_ctl_elem_value *value)
  450. {
  451. struct fwspk *fwspk = control->private_data;
  452. unsigned int i;
  453. for (i = 0; i < fwspk->device_info->mixer_channels; ++i)
  454. value->value.integer.value[channel_map[i]] = fwspk->volume[i];
  455. return 0;
  456. }
  457. static int fwspk_volume_put(struct snd_kcontrol *control,
  458. struct snd_ctl_elem_value *value)
  459. {
  460. struct fwspk *fwspk = control->private_data;
  461. unsigned int i, changed_channels;
  462. bool equal_values = true;
  463. s16 volume;
  464. int err;
  465. for (i = 0; i < fwspk->device_info->mixer_channels; ++i) {
  466. if (value->value.integer.value[i] < fwspk->volume_min ||
  467. value->value.integer.value[i] > fwspk->volume_max)
  468. return -EINVAL;
  469. if (value->value.integer.value[i] !=
  470. value->value.integer.value[0])
  471. equal_values = false;
  472. }
  473. changed_channels = 0;
  474. for (i = 0; i < fwspk->device_info->mixer_channels; ++i)
  475. if (value->value.integer.value[channel_map[i]] !=
  476. fwspk->volume[i])
  477. changed_channels |= 1 << (i + 1);
  478. if (equal_values && changed_channels != 0)
  479. changed_channels = 1 << 0;
  480. for (i = 0; i <= fwspk->device_info->mixer_channels; ++i) {
  481. volume = value->value.integer.value[channel_map[i ? i - 1 : 0]];
  482. if (changed_channels & (1 << i)) {
  483. err = fwspk_volume_command(fwspk, &volume, i,
  484. CTL_CURRENT, CTL_WRITE);
  485. if (err < 0)
  486. return err;
  487. }
  488. if (i > 0)
  489. fwspk->volume[i - 1] = volume;
  490. }
  491. return changed_channels != 0;
  492. }
  493. static int fwspk_create_mixer(struct fwspk *fwspk)
  494. {
  495. static const struct snd_kcontrol_new controls[] = {
  496. {
  497. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  498. .name = "PCM Playback Switch",
  499. .info = snd_ctl_boolean_mono_info,
  500. .get = fwspk_mute_get,
  501. .put = fwspk_mute_put,
  502. },
  503. {
  504. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  505. .name = "PCM Playback Volume",
  506. .info = fwspk_volume_info,
  507. .get = fwspk_volume_get,
  508. .put = fwspk_volume_put,
  509. },
  510. };
  511. unsigned int i, first_ch;
  512. int err;
  513. err = fwspk_volume_command(fwspk, &fwspk->volume_min,
  514. 0, CTL_MIN, CTL_READ);
  515. if (err < 0)
  516. return err;
  517. err = fwspk_volume_command(fwspk, &fwspk->volume_max,
  518. 0, CTL_MAX, CTL_READ);
  519. if (err < 0)
  520. return err;
  521. err = fwspk_mute_command(fwspk, &fwspk->mute, CTL_READ);
  522. if (err < 0)
  523. return err;
  524. first_ch = fwspk->device_info->mixer_channels == 1 ? 0 : 1;
  525. for (i = 0; i < fwspk->device_info->mixer_channels; ++i) {
  526. err = fwspk_volume_command(fwspk, &fwspk->volume[i],
  527. first_ch + i, CTL_CURRENT, CTL_READ);
  528. if (err < 0)
  529. return err;
  530. }
  531. for (i = 0; i < ARRAY_SIZE(controls); ++i) {
  532. err = snd_ctl_add(fwspk->card,
  533. snd_ctl_new1(&controls[i], fwspk));
  534. if (err < 0)
  535. return err;
  536. }
  537. return 0;
  538. }
  539. static u32 fwspk_read_firmware_version(struct fw_unit *unit)
  540. {
  541. __be32 data;
  542. int err;
  543. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  544. OXFORD_FIRMWARE_ID_ADDRESS, &data, 4);
  545. return err >= 0 ? be32_to_cpu(data) : 0;
  546. }
  547. static void fwspk_card_free(struct snd_card *card)
  548. {
  549. struct fwspk *fwspk = card->private_data;
  550. amdtp_out_stream_destroy(&fwspk->stream);
  551. cmp_connection_destroy(&fwspk->connection);
  552. fw_unit_put(fwspk->unit);
  553. mutex_destroy(&fwspk->mutex);
  554. }
  555. static int fwspk_probe(struct fw_unit *unit,
  556. const struct ieee1394_device_id *id)
  557. {
  558. struct fw_device *fw_dev = fw_parent_device(unit);
  559. struct snd_card *card;
  560. struct fwspk *fwspk;
  561. u32 firmware;
  562. int err;
  563. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*fwspk), &card);
  564. if (err < 0)
  565. return err;
  566. snd_card_set_dev(card, &unit->device);
  567. fwspk = card->private_data;
  568. fwspk->card = card;
  569. mutex_init(&fwspk->mutex);
  570. fwspk->unit = fw_unit_get(unit);
  571. fwspk->device_info = (const struct device_info *)id->driver_data;
  572. err = cmp_connection_init(&fwspk->connection, unit, 0);
  573. if (err < 0)
  574. goto err_unit;
  575. err = amdtp_out_stream_init(&fwspk->stream, unit, CIP_NONBLOCKING);
  576. if (err < 0)
  577. goto err_connection;
  578. card->private_free = fwspk_card_free;
  579. strcpy(card->driver, fwspk->device_info->driver_name);
  580. strcpy(card->shortname, fwspk->device_info->short_name);
  581. firmware = fwspk_read_firmware_version(unit);
  582. snprintf(card->longname, sizeof(card->longname),
  583. "%s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
  584. fwspk->device_info->long_name,
  585. firmware >> 20, firmware & 0xffff,
  586. fw_dev->config_rom[3], fw_dev->config_rom[4],
  587. dev_name(&unit->device), 100 << fw_dev->max_speed);
  588. strcpy(card->mixername, "OXFW970");
  589. err = fwspk_create_pcm(fwspk);
  590. if (err < 0)
  591. goto error;
  592. err = fwspk_create_mixer(fwspk);
  593. if (err < 0)
  594. goto error;
  595. err = snd_card_register(card);
  596. if (err < 0)
  597. goto error;
  598. dev_set_drvdata(&unit->device, fwspk);
  599. return 0;
  600. err_connection:
  601. cmp_connection_destroy(&fwspk->connection);
  602. err_unit:
  603. fw_unit_put(fwspk->unit);
  604. mutex_destroy(&fwspk->mutex);
  605. error:
  606. snd_card_free(card);
  607. return err;
  608. }
  609. static void fwspk_bus_reset(struct fw_unit *unit)
  610. {
  611. struct fwspk *fwspk = dev_get_drvdata(&unit->device);
  612. fcp_bus_reset(fwspk->unit);
  613. if (cmp_connection_update(&fwspk->connection) < 0) {
  614. amdtp_out_stream_pcm_abort(&fwspk->stream);
  615. mutex_lock(&fwspk->mutex);
  616. fwspk_stop_stream(fwspk);
  617. mutex_unlock(&fwspk->mutex);
  618. return;
  619. }
  620. amdtp_out_stream_update(&fwspk->stream);
  621. }
  622. static void fwspk_remove(struct fw_unit *unit)
  623. {
  624. struct fwspk *fwspk = dev_get_drvdata(&unit->device);
  625. amdtp_out_stream_pcm_abort(&fwspk->stream);
  626. snd_card_disconnect(fwspk->card);
  627. mutex_lock(&fwspk->mutex);
  628. fwspk_stop_stream(fwspk);
  629. mutex_unlock(&fwspk->mutex);
  630. snd_card_free_when_closed(fwspk->card);
  631. }
  632. static const struct device_info griffin_firewave = {
  633. .driver_name = "FireWave",
  634. .short_name = "FireWave",
  635. .long_name = "Griffin FireWave Surround",
  636. .pcm_constraints = firewave_constraints,
  637. .mixer_channels = 6,
  638. .mute_fb_id = 0x01,
  639. .volume_fb_id = 0x02,
  640. };
  641. static const struct device_info lacie_speakers = {
  642. .driver_name = "FWSpeakers",
  643. .short_name = "FireWire Speakers",
  644. .long_name = "LaCie FireWire Speakers",
  645. .pcm_constraints = lacie_speakers_constraints,
  646. .mixer_channels = 1,
  647. .mute_fb_id = 0x01,
  648. .volume_fb_id = 0x01,
  649. };
  650. static const struct ieee1394_device_id fwspk_id_table[] = {
  651. {
  652. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  653. IEEE1394_MATCH_MODEL_ID |
  654. IEEE1394_MATCH_SPECIFIER_ID |
  655. IEEE1394_MATCH_VERSION,
  656. .vendor_id = VENDOR_GRIFFIN,
  657. .model_id = 0x00f970,
  658. .specifier_id = SPECIFIER_1394TA,
  659. .version = VERSION_AVC,
  660. .driver_data = (kernel_ulong_t)&griffin_firewave,
  661. },
  662. {
  663. .match_flags = IEEE1394_MATCH_VENDOR_ID |
  664. IEEE1394_MATCH_MODEL_ID |
  665. IEEE1394_MATCH_SPECIFIER_ID |
  666. IEEE1394_MATCH_VERSION,
  667. .vendor_id = VENDOR_LACIE,
  668. .model_id = 0x00f970,
  669. .specifier_id = SPECIFIER_1394TA,
  670. .version = VERSION_AVC,
  671. .driver_data = (kernel_ulong_t)&lacie_speakers,
  672. },
  673. { }
  674. };
  675. MODULE_DEVICE_TABLE(ieee1394, fwspk_id_table);
  676. static struct fw_driver fwspk_driver = {
  677. .driver = {
  678. .owner = THIS_MODULE,
  679. .name = KBUILD_MODNAME,
  680. .bus = &fw_bus_type,
  681. },
  682. .probe = fwspk_probe,
  683. .update = fwspk_bus_reset,
  684. .remove = fwspk_remove,
  685. .id_table = fwspk_id_table,
  686. };
  687. static int __init alsa_fwspk_init(void)
  688. {
  689. return driver_register(&fwspk_driver.driver);
  690. }
  691. static void __exit alsa_fwspk_exit(void)
  692. {
  693. driver_unregister(&fwspk_driver.driver);
  694. }
  695. module_init(alsa_fwspk_init);
  696. module_exit(alsa_fwspk_exit);