speakers.c 20 KB

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