isight.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. unsigned long flags;
  117. if (ACCESS_ONCE(isight->pcm_active)) {
  118. snd_pcm_stream_lock_irqsave(isight->pcm, flags);
  119. if (snd_pcm_running(isight->pcm))
  120. snd_pcm_stop(isight->pcm, SNDRV_PCM_STATE_XRUN);
  121. snd_pcm_stream_unlock_irqrestore(isight->pcm, flags);
  122. }
  123. }
  124. static void isight_dropped_samples(struct isight *isight, unsigned int total)
  125. {
  126. struct snd_pcm_runtime *runtime;
  127. u32 dropped;
  128. unsigned int count1;
  129. if (!ACCESS_ONCE(isight->pcm_running))
  130. return;
  131. runtime = isight->pcm->runtime;
  132. dropped = total - isight->total_samples;
  133. if (dropped < runtime->buffer_size) {
  134. if (isight->buffer_pointer + dropped <= runtime->buffer_size) {
  135. memset(runtime->dma_area + isight->buffer_pointer * 4,
  136. 0, dropped * 4);
  137. } else {
  138. count1 = runtime->buffer_size - isight->buffer_pointer;
  139. memset(runtime->dma_area + isight->buffer_pointer * 4,
  140. 0, count1 * 4);
  141. memset(runtime->dma_area, 0, (dropped - count1) * 4);
  142. }
  143. isight_update_pointers(isight, dropped);
  144. } else {
  145. isight_pcm_abort(isight);
  146. }
  147. }
  148. static void isight_packet(struct fw_iso_context *context, u32 cycle,
  149. size_t header_length, void *header, void *data)
  150. {
  151. struct isight *isight = data;
  152. const struct audio_payload *payload;
  153. unsigned int index, length, count, total;
  154. int err;
  155. if (isight->packet_index < 0)
  156. return;
  157. index = isight->packet_index;
  158. payload = isight->buffer.packets[index].buffer;
  159. length = be32_to_cpup(header) >> 16;
  160. if (likely(length >= 16 &&
  161. payload->signature == cpu_to_be32(0x73676874/*"sght"*/))) {
  162. count = be32_to_cpu(payload->sample_count);
  163. if (likely(count <= (length - 16) / 4)) {
  164. total = be32_to_cpu(payload->sample_total);
  165. if (unlikely(total != isight->total_samples)) {
  166. if (!isight->first_packet)
  167. isight_dropped_samples(isight, total);
  168. isight->first_packet = false;
  169. isight->total_samples = total;
  170. }
  171. isight_samples(isight, payload->samples, count);
  172. isight->total_samples += count;
  173. }
  174. }
  175. err = fw_iso_context_queue(isight->context, &audio_packet,
  176. &isight->buffer.iso_buffer,
  177. isight->buffer.packets[index].offset);
  178. if (err < 0) {
  179. dev_err(&isight->unit->device, "queueing error: %d\n", err);
  180. isight_pcm_abort(isight);
  181. isight->packet_index = -1;
  182. return;
  183. }
  184. fw_iso_context_queue_flush(isight->context);
  185. if (++index >= QUEUE_LENGTH)
  186. index = 0;
  187. isight->packet_index = index;
  188. }
  189. static int isight_connect(struct isight *isight)
  190. {
  191. int ch, err, rcode, errors = 0;
  192. __be32 value;
  193. retry_after_bus_reset:
  194. ch = fw_iso_resources_allocate(&isight->resources,
  195. sizeof(struct audio_payload),
  196. isight->device->max_speed);
  197. if (ch < 0) {
  198. err = ch;
  199. goto error;
  200. }
  201. value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
  202. for (;;) {
  203. rcode = fw_run_transaction(
  204. isight->device->card,
  205. TCODE_WRITE_QUADLET_REQUEST,
  206. isight->device->node_id,
  207. isight->resources.generation,
  208. isight->device->max_speed,
  209. isight->audio_base + REG_ISO_TX_CONFIG,
  210. &value, 4);
  211. if (rcode == RCODE_COMPLETE) {
  212. return 0;
  213. } else if (rcode == RCODE_GENERATION) {
  214. fw_iso_resources_free(&isight->resources);
  215. goto retry_after_bus_reset;
  216. } else if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  217. err = -EIO;
  218. goto err_resources;
  219. }
  220. msleep(5);
  221. }
  222. err_resources:
  223. fw_iso_resources_free(&isight->resources);
  224. error:
  225. return err;
  226. }
  227. static int isight_open(struct snd_pcm_substream *substream)
  228. {
  229. static const struct snd_pcm_hardware hardware = {
  230. .info = SNDRV_PCM_INFO_MMAP |
  231. SNDRV_PCM_INFO_MMAP_VALID |
  232. SNDRV_PCM_INFO_BATCH |
  233. SNDRV_PCM_INFO_INTERLEAVED |
  234. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  235. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  236. .rates = SNDRV_PCM_RATE_48000,
  237. .rate_min = 48000,
  238. .rate_max = 48000,
  239. .channels_min = 2,
  240. .channels_max = 2,
  241. .buffer_bytes_max = 4 * 1024 * 1024,
  242. .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
  243. .period_bytes_max = 1024 * 1024,
  244. .periods_min = 2,
  245. .periods_max = UINT_MAX,
  246. };
  247. struct isight *isight = substream->private_data;
  248. substream->runtime->hw = hardware;
  249. return iso_packets_buffer_init(&isight->buffer, isight->unit,
  250. QUEUE_LENGTH,
  251. sizeof(struct audio_payload),
  252. DMA_FROM_DEVICE);
  253. }
  254. static int isight_close(struct snd_pcm_substream *substream)
  255. {
  256. struct isight *isight = substream->private_data;
  257. iso_packets_buffer_destroy(&isight->buffer, isight->unit);
  258. return 0;
  259. }
  260. static int isight_hw_params(struct snd_pcm_substream *substream,
  261. struct snd_pcm_hw_params *hw_params)
  262. {
  263. struct isight *isight = substream->private_data;
  264. int err;
  265. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  266. params_buffer_bytes(hw_params));
  267. if (err < 0)
  268. return err;
  269. ACCESS_ONCE(isight->pcm_active) = true;
  270. return 0;
  271. }
  272. static int reg_read(struct isight *isight, int offset, __be32 *value)
  273. {
  274. return snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  275. isight->audio_base + offset, value, 4);
  276. }
  277. static int reg_write(struct isight *isight, int offset, __be32 value)
  278. {
  279. return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  280. isight->audio_base + offset, &value, 4);
  281. }
  282. static void isight_stop_streaming(struct isight *isight)
  283. {
  284. if (!isight->context)
  285. return;
  286. fw_iso_context_stop(isight->context);
  287. fw_iso_context_destroy(isight->context);
  288. isight->context = NULL;
  289. fw_iso_resources_free(&isight->resources);
  290. reg_write(isight, REG_AUDIO_ENABLE, 0);
  291. }
  292. static int isight_hw_free(struct snd_pcm_substream *substream)
  293. {
  294. struct isight *isight = substream->private_data;
  295. ACCESS_ONCE(isight->pcm_active) = false;
  296. mutex_lock(&isight->mutex);
  297. isight_stop_streaming(isight);
  298. mutex_unlock(&isight->mutex);
  299. return snd_pcm_lib_free_vmalloc_buffer(substream);
  300. }
  301. static int isight_start_streaming(struct isight *isight)
  302. {
  303. unsigned int i;
  304. int err;
  305. if (isight->context) {
  306. if (isight->packet_index < 0)
  307. isight_stop_streaming(isight);
  308. else
  309. return 0;
  310. }
  311. err = reg_write(isight, REG_SAMPLE_RATE, cpu_to_be32(RATE_48000));
  312. if (err < 0)
  313. goto error;
  314. err = isight_connect(isight);
  315. if (err < 0)
  316. goto error;
  317. err = reg_write(isight, REG_AUDIO_ENABLE, cpu_to_be32(AUDIO_ENABLE));
  318. if (err < 0)
  319. goto err_resources;
  320. isight->context = fw_iso_context_create(isight->device->card,
  321. FW_ISO_CONTEXT_RECEIVE,
  322. isight->resources.channel,
  323. isight->device->max_speed,
  324. 4, isight_packet, isight);
  325. if (IS_ERR(isight->context)) {
  326. err = PTR_ERR(isight->context);
  327. isight->context = NULL;
  328. goto err_resources;
  329. }
  330. for (i = 0; i < QUEUE_LENGTH; ++i) {
  331. err = fw_iso_context_queue(isight->context, &audio_packet,
  332. &isight->buffer.iso_buffer,
  333. isight->buffer.packets[i].offset);
  334. if (err < 0)
  335. goto err_context;
  336. }
  337. isight->first_packet = true;
  338. isight->packet_index = 0;
  339. err = fw_iso_context_start(isight->context, -1, 0,
  340. FW_ISO_CONTEXT_MATCH_ALL_TAGS/*?*/);
  341. if (err < 0)
  342. goto err_context;
  343. return 0;
  344. err_context:
  345. fw_iso_context_destroy(isight->context);
  346. isight->context = NULL;
  347. err_resources:
  348. fw_iso_resources_free(&isight->resources);
  349. reg_write(isight, REG_AUDIO_ENABLE, 0);
  350. error:
  351. return err;
  352. }
  353. static int isight_prepare(struct snd_pcm_substream *substream)
  354. {
  355. struct isight *isight = substream->private_data;
  356. int err;
  357. isight->buffer_pointer = 0;
  358. isight->period_counter = 0;
  359. mutex_lock(&isight->mutex);
  360. err = isight_start_streaming(isight);
  361. mutex_unlock(&isight->mutex);
  362. return err;
  363. }
  364. static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
  365. {
  366. struct isight *isight = substream->private_data;
  367. switch (cmd) {
  368. case SNDRV_PCM_TRIGGER_START:
  369. ACCESS_ONCE(isight->pcm_running) = true;
  370. break;
  371. case SNDRV_PCM_TRIGGER_STOP:
  372. ACCESS_ONCE(isight->pcm_running) = false;
  373. break;
  374. default:
  375. return -EINVAL;
  376. }
  377. return 0;
  378. }
  379. static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
  380. {
  381. struct isight *isight = substream->private_data;
  382. return ACCESS_ONCE(isight->buffer_pointer);
  383. }
  384. static int isight_create_pcm(struct isight *isight)
  385. {
  386. static struct snd_pcm_ops ops = {
  387. .open = isight_open,
  388. .close = isight_close,
  389. .ioctl = snd_pcm_lib_ioctl,
  390. .hw_params = isight_hw_params,
  391. .hw_free = isight_hw_free,
  392. .prepare = isight_prepare,
  393. .trigger = isight_trigger,
  394. .pointer = isight_pointer,
  395. .page = snd_pcm_lib_get_vmalloc_page,
  396. .mmap = snd_pcm_lib_mmap_vmalloc,
  397. };
  398. struct snd_pcm *pcm;
  399. int err;
  400. err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
  401. if (err < 0)
  402. return err;
  403. pcm->private_data = isight;
  404. strcpy(pcm->name, "iSight");
  405. isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  406. isight->pcm->ops = &ops;
  407. return 0;
  408. }
  409. static int isight_gain_info(struct snd_kcontrol *ctl,
  410. struct snd_ctl_elem_info *info)
  411. {
  412. struct isight *isight = ctl->private_data;
  413. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  414. info->count = 1;
  415. info->value.integer.min = isight->gain_min;
  416. info->value.integer.max = isight->gain_max;
  417. return 0;
  418. }
  419. static int isight_gain_get(struct snd_kcontrol *ctl,
  420. struct snd_ctl_elem_value *value)
  421. {
  422. struct isight *isight = ctl->private_data;
  423. __be32 gain;
  424. int err;
  425. err = reg_read(isight, REG_GAIN, &gain);
  426. if (err < 0)
  427. return err;
  428. value->value.integer.value[0] = (s32)be32_to_cpu(gain);
  429. return 0;
  430. }
  431. static int isight_gain_put(struct snd_kcontrol *ctl,
  432. struct snd_ctl_elem_value *value)
  433. {
  434. struct isight *isight = ctl->private_data;
  435. if (value->value.integer.value[0] < isight->gain_min ||
  436. value->value.integer.value[0] > isight->gain_max)
  437. return -EINVAL;
  438. return reg_write(isight, REG_GAIN,
  439. cpu_to_be32(value->value.integer.value[0]));
  440. }
  441. static int isight_mute_get(struct snd_kcontrol *ctl,
  442. struct snd_ctl_elem_value *value)
  443. {
  444. struct isight *isight = ctl->private_data;
  445. __be32 mute;
  446. int err;
  447. err = reg_read(isight, REG_MUTE, &mute);
  448. if (err < 0)
  449. return err;
  450. value->value.integer.value[0] = !mute;
  451. return 0;
  452. }
  453. static int isight_mute_put(struct snd_kcontrol *ctl,
  454. struct snd_ctl_elem_value *value)
  455. {
  456. struct isight *isight = ctl->private_data;
  457. return reg_write(isight, REG_MUTE,
  458. (__force __be32)!value->value.integer.value[0]);
  459. }
  460. static int isight_create_mixer(struct isight *isight)
  461. {
  462. static const struct snd_kcontrol_new gain_control = {
  463. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  464. .name = "Mic Capture Volume",
  465. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  466. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  467. .info = isight_gain_info,
  468. .get = isight_gain_get,
  469. .put = isight_gain_put,
  470. };
  471. static const struct snd_kcontrol_new mute_control = {
  472. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  473. .name = "Mic Capture Switch",
  474. .info = snd_ctl_boolean_mono_info,
  475. .get = isight_mute_get,
  476. .put = isight_mute_put,
  477. };
  478. __be32 value;
  479. struct snd_kcontrol *ctl;
  480. int err;
  481. err = reg_read(isight, REG_GAIN_RAW_START, &value);
  482. if (err < 0)
  483. return err;
  484. isight->gain_min = be32_to_cpu(value);
  485. err = reg_read(isight, REG_GAIN_RAW_END, &value);
  486. if (err < 0)
  487. return err;
  488. isight->gain_max = be32_to_cpu(value);
  489. isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
  490. isight->gain_tlv[1] = 2 * sizeof(unsigned int);
  491. err = reg_read(isight, REG_GAIN_DB_START, &value);
  492. if (err < 0)
  493. return err;
  494. isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
  495. err = reg_read(isight, REG_GAIN_DB_END, &value);
  496. if (err < 0)
  497. return err;
  498. isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
  499. ctl = snd_ctl_new1(&gain_control, isight);
  500. if (ctl)
  501. ctl->tlv.p = isight->gain_tlv;
  502. err = snd_ctl_add(isight->card, ctl);
  503. if (err < 0)
  504. return err;
  505. err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
  506. if (err < 0)
  507. return err;
  508. return 0;
  509. }
  510. static void isight_card_free(struct snd_card *card)
  511. {
  512. struct isight *isight = card->private_data;
  513. fw_iso_resources_destroy(&isight->resources);
  514. fw_unit_put(isight->unit);
  515. mutex_destroy(&isight->mutex);
  516. }
  517. static u64 get_unit_base(struct fw_unit *unit)
  518. {
  519. struct fw_csr_iterator i;
  520. int key, value;
  521. fw_csr_iterator_init(&i, unit->directory);
  522. while (fw_csr_iterator_next(&i, &key, &value))
  523. if (key == CSR_OFFSET)
  524. return CSR_REGISTER_BASE + value * 4;
  525. return 0;
  526. }
  527. static int isight_probe(struct device *unit_dev)
  528. {
  529. struct fw_unit *unit = fw_unit(unit_dev);
  530. struct fw_device *fw_dev = fw_parent_device(unit);
  531. struct snd_card *card;
  532. struct isight *isight;
  533. int err;
  534. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*isight), &card);
  535. if (err < 0)
  536. return err;
  537. snd_card_set_dev(card, unit_dev);
  538. isight = card->private_data;
  539. isight->card = card;
  540. mutex_init(&isight->mutex);
  541. isight->unit = fw_unit_get(unit);
  542. isight->device = fw_dev;
  543. isight->audio_base = get_unit_base(unit);
  544. if (!isight->audio_base) {
  545. dev_err(&unit->device, "audio unit base not found\n");
  546. err = -ENXIO;
  547. goto err_unit;
  548. }
  549. fw_iso_resources_init(&isight->resources, unit);
  550. card->private_free = isight_card_free;
  551. strcpy(card->driver, "iSight");
  552. strcpy(card->shortname, "Apple iSight");
  553. snprintf(card->longname, sizeof(card->longname),
  554. "Apple iSight (GUID %08x%08x) at %s, S%d",
  555. fw_dev->config_rom[3], fw_dev->config_rom[4],
  556. dev_name(&unit->device), 100 << fw_dev->max_speed);
  557. strcpy(card->mixername, "iSight");
  558. err = isight_create_pcm(isight);
  559. if (err < 0)
  560. goto error;
  561. err = isight_create_mixer(isight);
  562. if (err < 0)
  563. goto error;
  564. err = snd_card_register(card);
  565. if (err < 0)
  566. goto error;
  567. dev_set_drvdata(unit_dev, isight);
  568. return 0;
  569. err_unit:
  570. fw_unit_put(isight->unit);
  571. mutex_destroy(&isight->mutex);
  572. error:
  573. snd_card_free(card);
  574. return err;
  575. }
  576. static int isight_remove(struct device *dev)
  577. {
  578. struct isight *isight = dev_get_drvdata(dev);
  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. return 0;
  586. }
  587. static void isight_bus_reset(struct fw_unit *unit)
  588. {
  589. struct isight *isight = dev_get_drvdata(&unit->device);
  590. if (fw_iso_resources_update(&isight->resources) < 0) {
  591. isight_pcm_abort(isight);
  592. mutex_lock(&isight->mutex);
  593. isight_stop_streaming(isight);
  594. mutex_unlock(&isight->mutex);
  595. }
  596. }
  597. static const struct ieee1394_device_id isight_id_table[] = {
  598. {
  599. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  600. IEEE1394_MATCH_VERSION,
  601. .specifier_id = OUI_APPLE,
  602. .version = SW_ISIGHT_AUDIO,
  603. },
  604. { }
  605. };
  606. MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
  607. static struct fw_driver isight_driver = {
  608. .driver = {
  609. .owner = THIS_MODULE,
  610. .name = KBUILD_MODNAME,
  611. .bus = &fw_bus_type,
  612. .probe = isight_probe,
  613. .remove = isight_remove,
  614. },
  615. .update = isight_bus_reset,
  616. .id_table = isight_id_table,
  617. };
  618. static int __init alsa_isight_init(void)
  619. {
  620. return driver_register(&isight_driver.driver);
  621. }
  622. static void __exit alsa_isight_exit(void)
  623. {
  624. driver_unregister(&isight_driver.driver);
  625. }
  626. module_init(alsa_isight_init);
  627. module_exit(alsa_isight_exit);