isight.c 18 KB

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