isight.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Apple iSight audio 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 <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 v2");
  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. ACCESS_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 (!ACCESS_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 (ACCESS_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 (!ACCESS_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. int err;
  251. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  252. params_buffer_bytes(hw_params));
  253. if (err < 0)
  254. return err;
  255. ACCESS_ONCE(isight->pcm_active) = true;
  256. return 0;
  257. }
  258. static int reg_read(struct isight *isight, int offset, __be32 *value)
  259. {
  260. return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  261. isight->audio_base + offset, value, 4, 0);
  262. }
  263. static int reg_write(struct isight *isight, int offset, __be32 value)
  264. {
  265. return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  266. isight->audio_base + offset, &value, 4, 0);
  267. }
  268. static void isight_stop_streaming(struct isight *isight)
  269. {
  270. __be32 value;
  271. if (!isight->context)
  272. return;
  273. fw_iso_context_stop(isight->context);
  274. fw_iso_context_destroy(isight->context);
  275. isight->context = NULL;
  276. fw_iso_resources_free(&isight->resources);
  277. value = 0;
  278. snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  279. isight->audio_base + REG_AUDIO_ENABLE,
  280. &value, 4, FW_QUIET);
  281. }
  282. static int isight_hw_free(struct snd_pcm_substream *substream)
  283. {
  284. struct isight *isight = substream->private_data;
  285. ACCESS_ONCE(isight->pcm_active) = false;
  286. mutex_lock(&isight->mutex);
  287. isight_stop_streaming(isight);
  288. mutex_unlock(&isight->mutex);
  289. return snd_pcm_lib_free_vmalloc_buffer(substream);
  290. }
  291. static int isight_start_streaming(struct isight *isight)
  292. {
  293. unsigned int i;
  294. int err;
  295. if (isight->context) {
  296. if (isight->packet_index < 0)
  297. isight_stop_streaming(isight);
  298. else
  299. return 0;
  300. }
  301. err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000));
  302. if (err < 0)
  303. goto error;
  304. err = isight_connect(isight);
  305. if (err < 0)
  306. goto error;
  307. err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE));
  308. if (err < 0)
  309. goto err_resources;
  310. isight->context = fw_iso_context_create(isight->device->card,
  311. FW_ISO_CONTEXT_RECEIVE,
  312. isight->resources.channel,
  313. isight->device->max_speed,
  314. 4, isight_packet, isight);
  315. if (IS_ERR(isight->context)) {
  316. err = PTR_ERR(isight->context);
  317. isight->context = NULL;
  318. goto err_resources;
  319. }
  320. for (i = 0; i < QUEUE_LENGTH; ++i) {
  321. err = fw_iso_context_queue(isight->context, &audio_packet,
  322. &isight->buffer.iso_buffer,
  323. isight->buffer.packets[i].offset);
  324. if (err < 0)
  325. goto err_context;
  326. }
  327. isight->first_packet = true;
  328. isight->packet_index = 0;
  329. err = fw_iso_context_start(isight->context, -1, 0,
  330. FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/);
  331. if (err < 0)
  332. goto err_context;
  333. return 0;
  334. err_context:
  335. fw_iso_context_destroy(isight->context);
  336. isight->context = NULL;
  337. err_resources:
  338. fw_iso_resources_free(&isight->resources);
  339. reg_write(isight, REG_AUDIO_ENABLE, 0);
  340. error:
  341. return err;
  342. }
  343. static int isight_prepare(struct snd_pcm_substream *substream)
  344. {
  345. struct isight *isight = substream->private_data;
  346. int err;
  347. isight->buffer_pointer = 0;
  348. isight->period_counter = 0;
  349. mutex_lock(&isight->mutex);
  350. err = isight_start_streaming(isight);
  351. mutex_unlock(&isight->mutex);
  352. return err;
  353. }
  354. static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
  355. {
  356. struct isight *isight = substream->private_data;
  357. switch (cmd) {
  358. case SNDRV_PCM_TRIGGER_START:
  359. ACCESS_ONCE(isight->pcm_running) = true;
  360. break;
  361. case SNDRV_PCM_TRIGGER_STOP:
  362. ACCESS_ONCE(isight->pcm_running) = false;
  363. break;
  364. default:
  365. return -EINVAL;
  366. }
  367. return 0;
  368. }
  369. static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
  370. {
  371. struct isight *isight = substream->private_data;
  372. return ACCESS_ONCE(isight->buffer_pointer);
  373. }
  374. static int isight_create_pcm(struct isight *isight)
  375. {
  376. static struct snd_pcm_ops ops = {
  377. .open = isight_open,
  378. .close = isight_close,
  379. .ioctl = snd_pcm_lib_ioctl,
  380. .hw_params = isight_hw_params,
  381. .hw_free = isight_hw_free,
  382. .prepare = isight_prepare,
  383. .trigger = isight_trigger,
  384. .pointer = isight_pointer,
  385. .page = snd_pcm_lib_get_vmalloc_page,
  386. .mmap = snd_pcm_lib_mmap_vmalloc,
  387. };
  388. struct snd_pcm *pcm;
  389. int err;
  390. err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
  391. if (err < 0)
  392. return err;
  393. pcm->private_data = isight;
  394. strcpy(pcm->name, "iSight");
  395. isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  396. isight->pcm->ops = &ops;
  397. return 0;
  398. }
  399. static int isight_gain_info(struct snd_kcontrol *ctl,
  400. struct snd_ctl_elem_info *info)
  401. {
  402. struct isight *isight = ctl->private_data;
  403. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  404. info->count = 1;
  405. info->value.integer.min = isight->gain_min;
  406. info->value.integer.max = isight->gain_max;
  407. return 0;
  408. }
  409. static int isight_gain_get(struct snd_kcontrol *ctl,
  410. struct snd_ctl_elem_value *value)
  411. {
  412. struct isight *isight = ctl->private_data;
  413. __be32 gain;
  414. int err;
  415. err = reg_read(isight, REG_GAIN, &gain);
  416. if (err < 0)
  417. return err;
  418. value->value.integer.value[0] = (s32)be32_to_cpu(gain);
  419. return 0;
  420. }
  421. static int isight_gain_put(struct snd_kcontrol *ctl,
  422. struct snd_ctl_elem_value *value)
  423. {
  424. struct isight *isight = ctl->private_data;
  425. if (value->value.integer.value[0] < isight->gain_min ||
  426. value->value.integer.value[0] > isight->gain_max)
  427. return -EINVAL;
  428. return reg_write(isight, REG_GAIN,
  429. cpu_to_be32(value->value.integer.value[0]));
  430. }
  431. static int isight_mute_get(struct snd_kcontrol *ctl,
  432. struct snd_ctl_elem_value *value)
  433. {
  434. struct isight *isight = ctl->private_data;
  435. __be32 mute;
  436. int err;
  437. err = reg_read(isight, REG_MUTE, &mute);
  438. if (err < 0)
  439. return err;
  440. value->value.integer.value[0] = !mute;
  441. return 0;
  442. }
  443. static int isight_mute_put(struct snd_kcontrol *ctl,
  444. struct snd_ctl_elem_value *value)
  445. {
  446. struct isight *isight = ctl->private_data;
  447. return reg_write(isight, REG_MUTE,
  448. (__force __be32)!value->value.integer.value[0]);
  449. }
  450. static int isight_create_mixer(struct isight *isight)
  451. {
  452. static const struct snd_kcontrol_new gain_control = {
  453. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  454. .name = "Mic Capture Volume",
  455. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  456. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  457. .info = isight_gain_info,
  458. .get = isight_gain_get,
  459. .put = isight_gain_put,
  460. };
  461. static const struct snd_kcontrol_new mute_control = {
  462. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  463. .name = "Mic Capture Switch",
  464. .info = snd_ctl_boolean_mono_info,
  465. .get = isight_mute_get,
  466. .put = isight_mute_put,
  467. };
  468. __be32 value;
  469. struct snd_kcontrol *ctl;
  470. int err;
  471. err = reg_read(isight, REG_GAIN_RAW_START, &value);
  472. if (err < 0)
  473. return err;
  474. isight->gain_min = be32_to_cpu(value);
  475. err = reg_read(isight, REG_GAIN_RAW_END, &value);
  476. if (err < 0)
  477. return err;
  478. isight->gain_max = be32_to_cpu(value);
  479. isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
  480. isight->gain_tlv[1] = 2 * sizeof(unsigned int);
  481. err = reg_read(isight, REG_GAIN_DB_START, &value);
  482. if (err < 0)
  483. return err;
  484. isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
  485. err = reg_read(isight, REG_GAIN_DB_END, &value);
  486. if (err < 0)
  487. return err;
  488. isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
  489. ctl = snd_ctl_new1(&gain_control, isight);
  490. if (ctl)
  491. ctl->tlv.p = isight->gain_tlv;
  492. err = snd_ctl_add(isight->card, ctl);
  493. if (err < 0)
  494. return err;
  495. err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
  496. if (err < 0)
  497. return err;
  498. return 0;
  499. }
  500. static void isight_card_free(struct snd_card *card)
  501. {
  502. struct isight *isight = card->private_data;
  503. fw_iso_resources_destroy(&isight->resources);
  504. fw_unit_put(isight->unit);
  505. mutex_destroy(&isight->mutex);
  506. }
  507. static u64 get_unit_base(struct fw_unit *unit)
  508. {
  509. struct fw_csr_iterator i;
  510. int key, value;
  511. fw_csr_iterator_init(&i, unit->directory);
  512. while (fw_csr_iterator_next(&i, &key, &value))
  513. if (key == CSR_OFFSET)
  514. return CSR_REGISTER_BASE + value * 4;
  515. return 0;
  516. }
  517. static int isight_probe(struct fw_unit *unit,
  518. const struct ieee1394_device_id *id)
  519. {
  520. struct fw_device *fw_dev = fw_parent_device(unit);
  521. struct snd_card *card;
  522. struct isight *isight;
  523. int err;
  524. err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
  525. sizeof(*isight), &card);
  526. if (err < 0)
  527. return err;
  528. isight = card->private_data;
  529. isight->card = card;
  530. mutex_init(&isight->mutex);
  531. isight->unit = fw_unit_get(unit);
  532. isight->device = fw_dev;
  533. isight->audio_base = get_unit_base(unit);
  534. if (!isight->audio_base) {
  535. dev_err(&unit->device, "audio unit base not found\n");
  536. err = -ENXIO;
  537. goto err_unit;
  538. }
  539. fw_iso_resources_init(&isight->resources, unit);
  540. card->private_free = isight_card_free;
  541. strcpy(card->driver, "iSight");
  542. strcpy(card->shortname, "Apple iSight");
  543. snprintf(card->longname, sizeof(card->longname),
  544. "Apple iSight (GUID %08x%08x) at %s, S%d",
  545. fw_dev->config_rom[3], fw_dev->config_rom[4],
  546. dev_name(&unit->device), 100 << fw_dev->max_speed);
  547. strcpy(card->mixername, "iSight");
  548. err = isight_create_pcm(isight);
  549. if (err < 0)
  550. goto error;
  551. err = isight_create_mixer(isight);
  552. if (err < 0)
  553. goto error;
  554. err = snd_card_register(card);
  555. if (err < 0)
  556. goto error;
  557. dev_set_drvdata(&unit->device, isight);
  558. return 0;
  559. err_unit:
  560. fw_unit_put(isight->unit);
  561. mutex_destroy(&isight->mutex);
  562. error:
  563. snd_card_free(card);
  564. return err;
  565. }
  566. static void isight_bus_reset(struct fw_unit *unit)
  567. {
  568. struct isight *isight = dev_get_drvdata(&unit->device);
  569. if (fw_iso_resources_update(&isight->resources) < 0) {
  570. isight_pcm_abort(isight);
  571. mutex_lock(&isight->mutex);
  572. isight_stop_streaming(isight);
  573. mutex_unlock(&isight->mutex);
  574. }
  575. }
  576. static void isight_remove(struct fw_unit *unit)
  577. {
  578. struct isight *isight = dev_get_drvdata(&unit->device);
  579. isight_pcm_abort(isight);
  580. snd_card_disconnect(isight->card);
  581. mutex_lock(&isight->mutex);
  582. isight_stop_streaming(isight);
  583. mutex_unlock(&isight->mutex);
  584. snd_card_free_when_closed(isight->card);
  585. }
  586. static const struct ieee1394_device_id isight_id_table[] = {
  587. {
  588. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  589. IEEE1394_MATCH_VERSION,
  590. .specifier_id = OUI_APPLE,
  591. .version = SW_ISIGHT_AUDIO,
  592. },
  593. { }
  594. };
  595. MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
  596. static struct fw_driver isight_driver = {
  597. .driver = {
  598. .owner = THIS_MODULE,
  599. .name = KBUILD_MODNAME,
  600. .bus = &fw_bus_type,
  601. },
  602. .probe = isight_probe,
  603. .update = isight_bus_reset,
  604. .remove = isight_remove,
  605. .id_table = isight_id_table,
  606. };
  607. static int __init alsa_isight_init(void)
  608. {
  609. return driver_register(&isight_driver.driver);
  610. }
  611. static void __exit alsa_isight_exit(void)
  612. {
  613. driver_unregister(&isight_driver.driver);
  614. }
  615. module_init(alsa_isight_init);
  616. module_exit(alsa_isight_exit);