isight.c 18 KB

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