isight.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Apple iSight audio driver
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  6. */
  7. #include <asm/byteorder.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/firewire.h>
  11. #include <linux/firewire-constants.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/mutex.h>
  15. #include <linux/string.h>
  16. #include <sound/control.h>
  17. #include <sound/core.h>
  18. #include <sound/initval.h>
  19. #include <sound/pcm.h>
  20. #include <sound/tlv.h>
  21. #include "lib.h"
  22. #include "iso-resources.h"
  23. #include "packets-buffer.h"
  24. #define OUI_APPLE 0x000a27
  25. #define MODEL_APPLE_ISIGHT 0x000008
  26. #define SW_ISIGHT_AUDIO 0x000010
  27. #define REG_AUDIO_ENABLE 0x000
  28. #define AUDIO_ENABLE 0x80000000
  29. #define REG_DEF_AUDIO_GAIN 0x204
  30. #define REG_GAIN_RAW_START 0x210
  31. #define REG_GAIN_RAW_END 0x214
  32. #define REG_GAIN_DB_START 0x218
  33. #define REG_GAIN_DB_END 0x21c
  34. #define REG_SAMPLE_RATE_INQUIRY 0x280
  35. #define REG_ISO_TX_CONFIG 0x300
  36. #define SPEED_SHIFT 16
  37. #define REG_SAMPLE_RATE 0x400
  38. #define RATE_48000 0x80000000
  39. #define REG_GAIN 0x500
  40. #define REG_MUTE 0x504
  41. #define MAX_FRAMES_PER_PACKET 475
  42. #define QUEUE_LENGTH 20
  43. struct isight {
  44. struct snd_card *card;
  45. struct fw_unit *unit;
  46. struct fw_device *device;
  47. u64 audio_base;
  48. struct snd_pcm_substream *pcm;
  49. struct mutex mutex;
  50. struct iso_packets_buffer buffer;
  51. struct fw_iso_resources resources;
  52. struct fw_iso_context *context;
  53. bool pcm_active;
  54. bool pcm_running;
  55. bool first_packet;
  56. int packet_index;
  57. u32 total_samples;
  58. unsigned int buffer_pointer;
  59. unsigned int period_counter;
  60. s32 gain_min, gain_max;
  61. unsigned int gain_tlv[4];
  62. };
  63. struct audio_payload {
  64. __be32 sample_count;
  65. __be32 signature;
  66. __be32 sample_total;
  67. __be32 reserved;
  68. __be16 samples[2 * MAX_FRAMES_PER_PACKET];
  69. };
  70. MODULE_DESCRIPTION("iSight audio driver");
  71. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  72. MODULE_LICENSE("GPL");
  73. static struct fw_iso_packet audio_packet = {
  74. .payload_length = sizeof(struct audio_payload),
  75. .interrupt = 1,
  76. .header_length = 4,
  77. };
  78. static void isight_update_pointers(struct isight *isight, unsigned int count)
  79. {
  80. struct snd_pcm_runtime *runtime = isight->pcm->runtime;
  81. unsigned int ptr;
  82. smp_wmb(); /* update buffer data before buffer pointer */
  83. ptr = isight->buffer_pointer;
  84. ptr += count;
  85. if (ptr >= runtime->buffer_size)
  86. ptr -= runtime->buffer_size;
  87. WRITE_ONCE(isight->buffer_pointer, ptr);
  88. isight->period_counter += count;
  89. if (isight->period_counter >= runtime->period_size) {
  90. isight->period_counter -= runtime->period_size;
  91. snd_pcm_period_elapsed(isight->pcm);
  92. }
  93. }
  94. static void isight_samples(struct isight *isight,
  95. const __be16 *samples, unsigned int count)
  96. {
  97. struct snd_pcm_runtime *runtime;
  98. unsigned int count1;
  99. if (!READ_ONCE(isight->pcm_running))
  100. return;
  101. runtime = isight->pcm->runtime;
  102. if (isight->buffer_pointer + count <= runtime->buffer_size) {
  103. memcpy(runtime->dma_area + isight->buffer_pointer * 4,
  104. samples, count * 4);
  105. } else {
  106. count1 = runtime->buffer_size - isight->buffer_pointer;
  107. memcpy(runtime->dma_area + isight->buffer_pointer * 4,
  108. samples, count1 * 4);
  109. samples += count1 * 2;
  110. memcpy(runtime->dma_area, samples, (count - count1) * 4);
  111. }
  112. isight_update_pointers(isight, count);
  113. }
  114. static void isight_pcm_abort(struct isight *isight)
  115. {
  116. if (READ_ONCE(isight->pcm_active))
  117. snd_pcm_stop_xrun(isight->pcm);
  118. }
  119. static void isight_dropped_samples(struct isight *isight, unsigned int total)
  120. {
  121. struct snd_pcm_runtime *runtime;
  122. u32 dropped;
  123. unsigned int count1;
  124. if (!READ_ONCE(isight->pcm_running))
  125. return;
  126. runtime = isight->pcm->runtime;
  127. dropped = total - isight->total_samples;
  128. if (dropped < runtime->buffer_size) {
  129. if (isight->buffer_pointer + dropped <= runtime->buffer_size) {
  130. memset(runtime->dma_area + isight->buffer_pointer * 4,
  131. 0, dropped * 4);
  132. } else {
  133. count1 = runtime->buffer_size - isight->buffer_pointer;
  134. memset(runtime->dma_area + isight->buffer_pointer * 4,
  135. 0, count1 * 4);
  136. memset(runtime->dma_area, 0, (dropped - count1) * 4);
  137. }
  138. isight_update_pointers(isight, dropped);
  139. } else {
  140. isight_pcm_abort(isight);
  141. }
  142. }
  143. static void isight_packet(struct fw_iso_context *context, u32 cycle,
  144. size_t header_length, void *header, void *data)
  145. {
  146. struct isight *isight = data;
  147. const struct audio_payload *payload;
  148. unsigned int index, length, count, total;
  149. int err;
  150. if (isight->packet_index < 0)
  151. return;
  152. index = isight->packet_index;
  153. payload = isight->buffer.packets[index].buffer;
  154. length = be32_to_cpup(header) >> 16;
  155. if (likely(length >= 16 &&
  156. payload->signature == cpu_to_be32(0x73676874/*"sght"*/))) {
  157. count = be32_to_cpu(payload->sample_count);
  158. if (likely(count <= (length - 16) / 4)) {
  159. total = be32_to_cpu(payload->sample_total);
  160. if (unlikely(total != isight->total_samples)) {
  161. if (!isight->first_packet)
  162. isight_dropped_samples(isight, total);
  163. isight->first_packet = false;
  164. isight->total_samples = total;
  165. }
  166. isight_samples(isight, payload->samples, count);
  167. isight->total_samples += count;
  168. }
  169. }
  170. err = fw_iso_context_queue(isight->context, &audio_packet,
  171. &isight->buffer.iso_buffer,
  172. isight->buffer.packets[index].offset);
  173. if (err < 0) {
  174. dev_err(&isight->unit->device, "queueing error: %d\n", err);
  175. isight_pcm_abort(isight);
  176. isight->packet_index = -1;
  177. return;
  178. }
  179. fw_iso_context_queue_flush(isight->context);
  180. if (++index >= QUEUE_LENGTH)
  181. index = 0;
  182. isight->packet_index = index;
  183. }
  184. static int isight_connect(struct isight *isight)
  185. {
  186. int ch, err;
  187. __be32 value;
  188. retry_after_bus_reset:
  189. ch = fw_iso_resources_allocate(&isight->resources,
  190. sizeof(struct audio_payload),
  191. isight->device->max_speed);
  192. if (ch < 0) {
  193. err = ch;
  194. goto error;
  195. }
  196. value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
  197. err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  198. isight->audio_base + REG_ISO_TX_CONFIG,
  199. &value, 4, FW_FIXED_GENERATION |
  200. isight->resources.generation);
  201. if (err == -EAGAIN) {
  202. fw_iso_resources_free(&isight->resources);
  203. goto retry_after_bus_reset;
  204. } else if (err < 0) {
  205. goto err_resources;
  206. }
  207. return 0;
  208. err_resources:
  209. fw_iso_resources_free(&isight->resources);
  210. error:
  211. return err;
  212. }
  213. static int isight_open(struct snd_pcm_substream *substream)
  214. {
  215. static const struct snd_pcm_hardware hardware = {
  216. .info = SNDRV_PCM_INFO_MMAP |
  217. SNDRV_PCM_INFO_MMAP_VALID |
  218. SNDRV_PCM_INFO_BATCH |
  219. SNDRV_PCM_INFO_INTERLEAVED |
  220. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  221. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  222. .rates = SNDRV_PCM_RATE_48000,
  223. .rate_min = 48000,
  224. .rate_max = 48000,
  225. .channels_min = 2,
  226. .channels_max = 2,
  227. .buffer_bytes_max = 4 * 1024 * 1024,
  228. .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
  229. .period_bytes_max = 1024 * 1024,
  230. .periods_min = 2,
  231. .periods_max = UINT_MAX,
  232. };
  233. struct isight *isight = substream->private_data;
  234. substream->runtime->hw = hardware;
  235. return iso_packets_buffer_init(&isight->buffer, isight->unit,
  236. QUEUE_LENGTH,
  237. sizeof(struct audio_payload),
  238. DMA_FROM_DEVICE);
  239. }
  240. static int isight_close(struct snd_pcm_substream *substream)
  241. {
  242. struct isight *isight = substream->private_data;
  243. iso_packets_buffer_destroy(&isight->buffer, isight->unit);
  244. return 0;
  245. }
  246. static int isight_hw_params(struct snd_pcm_substream *substream,
  247. struct snd_pcm_hw_params *hw_params)
  248. {
  249. struct isight *isight = substream->private_data;
  250. WRITE_ONCE(isight->pcm_active, true);
  251. return 0;
  252. }
  253. static int reg_read(struct isight *isight, int offset, __be32 *value)
  254. {
  255. return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  256. isight->audio_base + offset, value, 4, 0);
  257. }
  258. static int reg_write(struct isight *isight, int offset, __be32 value)
  259. {
  260. return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  261. isight->audio_base + offset, &value, 4, 0);
  262. }
  263. static void isight_stop_streaming(struct isight *isight)
  264. {
  265. __be32 value;
  266. if (!isight->context)
  267. return;
  268. fw_iso_context_stop(isight->context);
  269. fw_iso_context_destroy(isight->context);
  270. isight->context = NULL;
  271. fw_iso_resources_free(&isight->resources);
  272. value = 0;
  273. snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  274. isight->audio_base + REG_AUDIO_ENABLE,
  275. &value, 4, FW_QUIET);
  276. }
  277. static int isight_hw_free(struct snd_pcm_substream *substream)
  278. {
  279. struct isight *isight = substream->private_data;
  280. WRITE_ONCE(isight->pcm_active, false);
  281. mutex_lock(&isight->mutex);
  282. isight_stop_streaming(isight);
  283. mutex_unlock(&isight->mutex);
  284. return 0;
  285. }
  286. static int isight_start_streaming(struct isight *isight)
  287. {
  288. unsigned int i;
  289. int err;
  290. if (isight->context) {
  291. if (isight->packet_index < 0)
  292. isight_stop_streaming(isight);
  293. else
  294. return 0;
  295. }
  296. err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000));
  297. if (err < 0)
  298. goto error;
  299. err = isight_connect(isight);
  300. if (err < 0)
  301. goto error;
  302. err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE));
  303. if (err < 0)
  304. goto err_resources;
  305. isight->context = fw_iso_context_create(isight->device->card,
  306. FW_ISO_CONTEXT_RECEIVE,
  307. isight->resources.channel,
  308. isight->device->max_speed,
  309. 4, isight_packet, isight);
  310. if (IS_ERR(isight->context)) {
  311. err = PTR_ERR(isight->context);
  312. isight->context = NULL;
  313. goto err_resources;
  314. }
  315. for (i = 0; i < QUEUE_LENGTH; ++i) {
  316. err = fw_iso_context_queue(isight->context, &audio_packet,
  317. &isight->buffer.iso_buffer,
  318. isight->buffer.packets[i].offset);
  319. if (err < 0)
  320. goto err_context;
  321. }
  322. isight->first_packet = true;
  323. isight->packet_index = 0;
  324. err = fw_iso_context_start(isight->context, -1, 0,
  325. FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/);
  326. if (err < 0)
  327. goto err_context;
  328. return 0;
  329. err_context:
  330. fw_iso_context_destroy(isight->context);
  331. isight->context = NULL;
  332. err_resources:
  333. fw_iso_resources_free(&isight->resources);
  334. reg_write(isight, REG_AUDIO_ENABLE, 0);
  335. error:
  336. return err;
  337. }
  338. static int isight_prepare(struct snd_pcm_substream *substream)
  339. {
  340. struct isight *isight = substream->private_data;
  341. int err;
  342. isight->buffer_pointer = 0;
  343. isight->period_counter = 0;
  344. mutex_lock(&isight->mutex);
  345. err = isight_start_streaming(isight);
  346. mutex_unlock(&isight->mutex);
  347. return err;
  348. }
  349. static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
  350. {
  351. struct isight *isight = substream->private_data;
  352. switch (cmd) {
  353. case SNDRV_PCM_TRIGGER_START:
  354. WRITE_ONCE(isight->pcm_running, true);
  355. break;
  356. case SNDRV_PCM_TRIGGER_STOP:
  357. WRITE_ONCE(isight->pcm_running, false);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. return 0;
  363. }
  364. static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
  365. {
  366. struct isight *isight = substream->private_data;
  367. return READ_ONCE(isight->buffer_pointer);
  368. }
  369. static int isight_create_pcm(struct isight *isight)
  370. {
  371. static const struct snd_pcm_ops ops = {
  372. .open = isight_open,
  373. .close = isight_close,
  374. .hw_params = isight_hw_params,
  375. .hw_free = isight_hw_free,
  376. .prepare = isight_prepare,
  377. .trigger = isight_trigger,
  378. .pointer = isight_pointer,
  379. };
  380. struct snd_pcm *pcm;
  381. int err;
  382. err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
  383. if (err < 0)
  384. return err;
  385. pcm->private_data = isight;
  386. pcm->nonatomic = true;
  387. strcpy(pcm->name, "iSight");
  388. isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  389. isight->pcm->ops = &ops;
  390. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  391. return 0;
  392. }
  393. static int isight_gain_info(struct snd_kcontrol *ctl,
  394. struct snd_ctl_elem_info *info)
  395. {
  396. struct isight *isight = ctl->private_data;
  397. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  398. info->count = 1;
  399. info->value.integer.min = isight->gain_min;
  400. info->value.integer.max = isight->gain_max;
  401. return 0;
  402. }
  403. static int isight_gain_get(struct snd_kcontrol *ctl,
  404. struct snd_ctl_elem_value *value)
  405. {
  406. struct isight *isight = ctl->private_data;
  407. __be32 gain;
  408. int err;
  409. err = reg_read(isight, REG_GAIN, &gain);
  410. if (err < 0)
  411. return err;
  412. value->value.integer.value[0] = (s32)be32_to_cpu(gain);
  413. return 0;
  414. }
  415. static int isight_gain_put(struct snd_kcontrol *ctl,
  416. struct snd_ctl_elem_value *value)
  417. {
  418. struct isight *isight = ctl->private_data;
  419. if (value->value.integer.value[0] < isight->gain_min ||
  420. value->value.integer.value[0] > isight->gain_max)
  421. return -EINVAL;
  422. return reg_write(isight, REG_GAIN,
  423. cpu_to_be32(value->value.integer.value[0]));
  424. }
  425. static int isight_mute_get(struct snd_kcontrol *ctl,
  426. struct snd_ctl_elem_value *value)
  427. {
  428. struct isight *isight = ctl->private_data;
  429. __be32 mute;
  430. int err;
  431. err = reg_read(isight, REG_MUTE, &mute);
  432. if (err < 0)
  433. return err;
  434. value->value.integer.value[0] = !mute;
  435. return 0;
  436. }
  437. static int isight_mute_put(struct snd_kcontrol *ctl,
  438. struct snd_ctl_elem_value *value)
  439. {
  440. struct isight *isight = ctl->private_data;
  441. return reg_write(isight, REG_MUTE,
  442. (__force __be32)!value->value.integer.value[0]);
  443. }
  444. static int isight_create_mixer(struct isight *isight)
  445. {
  446. static const struct snd_kcontrol_new gain_control = {
  447. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  448. .name = "Mic Capture Volume",
  449. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  450. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  451. .info = isight_gain_info,
  452. .get = isight_gain_get,
  453. .put = isight_gain_put,
  454. };
  455. static const struct snd_kcontrol_new mute_control = {
  456. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  457. .name = "Mic Capture Switch",
  458. .info = snd_ctl_boolean_mono_info,
  459. .get = isight_mute_get,
  460. .put = isight_mute_put,
  461. };
  462. __be32 value;
  463. struct snd_kcontrol *ctl;
  464. int err;
  465. err = reg_read(isight, REG_GAIN_RAW_START, &value);
  466. if (err < 0)
  467. return err;
  468. isight->gain_min = be32_to_cpu(value);
  469. err = reg_read(isight, REG_GAIN_RAW_END, &value);
  470. if (err < 0)
  471. return err;
  472. isight->gain_max = be32_to_cpu(value);
  473. isight->gain_tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_MINMAX;
  474. isight->gain_tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
  475. err = reg_read(isight, REG_GAIN_DB_START, &value);
  476. if (err < 0)
  477. return err;
  478. isight->gain_tlv[SNDRV_CTL_TLVO_DB_MINMAX_MIN] =
  479. (s32)be32_to_cpu(value) * 100;
  480. err = reg_read(isight, REG_GAIN_DB_END, &value);
  481. if (err < 0)
  482. return err;
  483. isight->gain_tlv[SNDRV_CTL_TLVO_DB_MINMAX_MAX] =
  484. (s32)be32_to_cpu(value) * 100;
  485. ctl = snd_ctl_new1(&gain_control, isight);
  486. if (ctl)
  487. ctl->tlv.p = isight->gain_tlv;
  488. err = snd_ctl_add(isight->card, ctl);
  489. if (err < 0)
  490. return err;
  491. err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
  492. if (err < 0)
  493. return err;
  494. return 0;
  495. }
  496. static void isight_card_free(struct snd_card *card)
  497. {
  498. struct isight *isight = card->private_data;
  499. fw_iso_resources_destroy(&isight->resources);
  500. }
  501. static u64 get_unit_base(struct fw_unit *unit)
  502. {
  503. struct fw_csr_iterator i;
  504. int key, value;
  505. fw_csr_iterator_init(&i, unit->directory);
  506. while (fw_csr_iterator_next(&i, &key, &value))
  507. if (key == CSR_OFFSET)
  508. return CSR_REGISTER_BASE + value * 4;
  509. return 0;
  510. }
  511. static int isight_probe(struct fw_unit *unit,
  512. const struct ieee1394_device_id *id)
  513. {
  514. struct fw_device *fw_dev = fw_parent_device(unit);
  515. struct snd_card *card;
  516. struct isight *isight;
  517. int err;
  518. err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
  519. sizeof(*isight), &card);
  520. if (err < 0)
  521. return err;
  522. isight = card->private_data;
  523. isight->card = card;
  524. mutex_init(&isight->mutex);
  525. isight->unit = fw_unit_get(unit);
  526. isight->device = fw_dev;
  527. isight->audio_base = get_unit_base(unit);
  528. if (!isight->audio_base) {
  529. dev_err(&unit->device, "audio unit base not found\n");
  530. err = -ENXIO;
  531. goto error;
  532. }
  533. fw_iso_resources_init(&isight->resources, unit);
  534. card->private_free = isight_card_free;
  535. strcpy(card->driver, "iSight");
  536. strcpy(card->shortname, "Apple iSight");
  537. snprintf(card->longname, sizeof(card->longname),
  538. "Apple iSight (GUID %08x%08x) at %s, S%d",
  539. fw_dev->config_rom[3], fw_dev->config_rom[4],
  540. dev_name(&unit->device), 100 << fw_dev->max_speed);
  541. strcpy(card->mixername, "iSight");
  542. err = isight_create_pcm(isight);
  543. if (err < 0)
  544. goto error;
  545. err = isight_create_mixer(isight);
  546. if (err < 0)
  547. goto error;
  548. err = snd_card_register(card);
  549. if (err < 0)
  550. goto error;
  551. dev_set_drvdata(&unit->device, isight);
  552. return 0;
  553. error:
  554. snd_card_free(card);
  555. mutex_destroy(&isight->mutex);
  556. fw_unit_put(isight->unit);
  557. return err;
  558. }
  559. static void isight_bus_reset(struct fw_unit *unit)
  560. {
  561. struct isight *isight = dev_get_drvdata(&unit->device);
  562. if (fw_iso_resources_update(&isight->resources) < 0) {
  563. isight_pcm_abort(isight);
  564. mutex_lock(&isight->mutex);
  565. isight_stop_streaming(isight);
  566. mutex_unlock(&isight->mutex);
  567. }
  568. }
  569. static void isight_remove(struct fw_unit *unit)
  570. {
  571. struct isight *isight = dev_get_drvdata(&unit->device);
  572. isight_pcm_abort(isight);
  573. snd_card_disconnect(isight->card);
  574. mutex_lock(&isight->mutex);
  575. isight_stop_streaming(isight);
  576. mutex_unlock(&isight->mutex);
  577. // Block till all of ALSA character devices are released.
  578. snd_card_free(isight->card);
  579. mutex_destroy(&isight->mutex);
  580. fw_unit_put(isight->unit);
  581. }
  582. static const struct ieee1394_device_id isight_id_table[] = {
  583. {
  584. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  585. IEEE1394_MATCH_VERSION,
  586. .specifier_id = OUI_APPLE,
  587. .version = SW_ISIGHT_AUDIO,
  588. },
  589. { }
  590. };
  591. MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
  592. static struct fw_driver isight_driver = {
  593. .driver = {
  594. .owner = THIS_MODULE,
  595. .name = KBUILD_MODNAME,
  596. .bus = &fw_bus_type,
  597. },
  598. .probe = isight_probe,
  599. .update = isight_bus_reset,
  600. .remove = isight_remove,
  601. .id_table = isight_id_table,
  602. };
  603. static int __init alsa_isight_init(void)
  604. {
  605. return driver_register(&isight_driver.driver);
  606. }
  607. static void __exit alsa_isight_exit(void)
  608. {
  609. driver_unregister(&isight_driver.driver);
  610. }
  611. module_init(alsa_isight_init);
  612. module_exit(alsa_isight_exit);