isight.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. .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. if (++index >= QUEUE_LENGTH)
  185. index = 0;
  186. isight->packet_index = index;
  187. }
  188. static int isight_connect(struct isight *isight)
  189. {
  190. int ch, err, rcode, errors = 0;
  191. __be32 value;
  192. retry_after_bus_reset:
  193. ch = fw_iso_resources_allocate(&isight->resources,
  194. sizeof(struct audio_payload),
  195. isight->device->max_speed);
  196. if (ch < 0) {
  197. err = ch;
  198. goto error;
  199. }
  200. value = cpu_to_be32(ch | (isight->device->max_speed << SPEED_SHIFT));
  201. for (;;) {
  202. rcode = fw_run_transaction(
  203. isight->device->card,
  204. TCODE_WRITE_QUADLET_REQUEST,
  205. isight->device->node_id,
  206. isight->resources.generation,
  207. isight->device->max_speed,
  208. isight->audio_base + REG_ISO_TX_CONFIG,
  209. &value, 4);
  210. if (rcode == RCODE_COMPLETE) {
  211. return 0;
  212. } else if (rcode == RCODE_GENERATION) {
  213. fw_iso_resources_free(&isight->resources);
  214. goto retry_after_bus_reset;
  215. } else if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
  216. err = -EIO;
  217. goto err_resources;
  218. }
  219. msleep(5);
  220. }
  221. err_resources:
  222. fw_iso_resources_free(&isight->resources);
  223. error:
  224. return err;
  225. }
  226. static int isight_open(struct snd_pcm_substream *substream)
  227. {
  228. static const struct snd_pcm_hardware hardware = {
  229. .info = SNDRV_PCM_INFO_MMAP |
  230. SNDRV_PCM_INFO_MMAP_VALID |
  231. SNDRV_PCM_INFO_BATCH |
  232. SNDRV_PCM_INFO_INTERLEAVED |
  233. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  234. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  235. .rates = SNDRV_PCM_RATE_48000,
  236. .rate_min = 48000,
  237. .rate_max = 48000,
  238. .channels_min = 2,
  239. .channels_max = 2,
  240. .buffer_bytes_max = 4 * 1024 * 1024,
  241. .period_bytes_min = MAX_FRAMES_PER_PACKET * 4,
  242. .period_bytes_max = 1024 * 1024,
  243. .periods_min = 2,
  244. .periods_max = UINT_MAX,
  245. };
  246. struct isight *isight = substream->private_data;
  247. substream->runtime->hw = hardware;
  248. return iso_packets_buffer_init(&isight->buffer, isight->unit,
  249. QUEUE_LENGTH,
  250. sizeof(struct audio_payload),
  251. DMA_FROM_DEVICE);
  252. }
  253. static int isight_close(struct snd_pcm_substream *substream)
  254. {
  255. struct isight *isight = substream->private_data;
  256. iso_packets_buffer_destroy(&isight->buffer, isight->unit);
  257. return 0;
  258. }
  259. static int isight_hw_params(struct snd_pcm_substream *substream,
  260. struct snd_pcm_hw_params *hw_params)
  261. {
  262. struct isight *isight = substream->private_data;
  263. int err;
  264. err = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  265. params_buffer_bytes(hw_params));
  266. if (err < 0)
  267. return err;
  268. ACCESS_ONCE(isight->pcm_active) = true;
  269. return 0;
  270. }
  271. static void isight_stop_streaming(struct isight *isight)
  272. {
  273. __be32 value;
  274. if (!isight->context)
  275. return;
  276. fw_iso_context_stop(isight->context);
  277. fw_iso_context_destroy(isight->context);
  278. isight->context = NULL;
  279. value = 0;
  280. snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  281. isight->audio_base + REG_AUDIO_ENABLE,
  282. &value, 4);
  283. fw_iso_resources_free(&isight->resources);
  284. }
  285. static int isight_hw_free(struct snd_pcm_substream *substream)
  286. {
  287. struct isight *isight = substream->private_data;
  288. ACCESS_ONCE(isight->pcm_active) = false;
  289. mutex_lock(&isight->mutex);
  290. isight_stop_streaming(isight);
  291. mutex_unlock(&isight->mutex);
  292. return snd_pcm_lib_free_vmalloc_buffer(substream);
  293. }
  294. static int isight_start_streaming(struct isight *isight)
  295. {
  296. __be32 value;
  297. unsigned int i;
  298. int err;
  299. if (isight->context) {
  300. if (isight->packet_index < 0)
  301. isight_stop_streaming(isight);
  302. else
  303. return 0;
  304. }
  305. value = cpu_to_be32(RATE_48000);
  306. err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  307. isight->audio_base + REG_SAMPLE_RATE,
  308. &value, 4);
  309. if (err < 0)
  310. return err;
  311. err = isight_connect(isight);
  312. if (err < 0)
  313. goto error;
  314. value = cpu_to_be32(AUDIO_ENABLE);
  315. err = snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  316. isight->audio_base + REG_AUDIO_ENABLE,
  317. &value, 4);
  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. value = 0;
  349. snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  350. isight->audio_base + REG_AUDIO_ENABLE,
  351. &value, 4);
  352. fw_iso_resources_free(&isight->resources);
  353. error:
  354. return err;
  355. }
  356. static int isight_prepare(struct snd_pcm_substream *substream)
  357. {
  358. struct isight *isight = substream->private_data;
  359. int err;
  360. isight->buffer_pointer = 0;
  361. isight->period_counter = 0;
  362. mutex_lock(&isight->mutex);
  363. err = isight_start_streaming(isight);
  364. mutex_unlock(&isight->mutex);
  365. return err;
  366. }
  367. static int isight_trigger(struct snd_pcm_substream *substream, int cmd)
  368. {
  369. struct isight *isight = substream->private_data;
  370. switch (cmd) {
  371. case SNDRV_PCM_TRIGGER_START:
  372. ACCESS_ONCE(isight->pcm_running) = true;
  373. break;
  374. case SNDRV_PCM_TRIGGER_STOP:
  375. ACCESS_ONCE(isight->pcm_running) = false;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. return 0;
  381. }
  382. static snd_pcm_uframes_t isight_pointer(struct snd_pcm_substream *substream)
  383. {
  384. struct isight *isight = substream->private_data;
  385. return ACCESS_ONCE(isight->buffer_pointer);
  386. }
  387. static int isight_create_pcm(struct isight *isight)
  388. {
  389. static struct snd_pcm_ops ops = {
  390. .open = isight_open,
  391. .close = isight_close,
  392. .ioctl = snd_pcm_lib_ioctl,
  393. .hw_params = isight_hw_params,
  394. .hw_free = isight_hw_free,
  395. .prepare = isight_prepare,
  396. .trigger = isight_trigger,
  397. .pointer = isight_pointer,
  398. .page = snd_pcm_lib_get_vmalloc_page,
  399. .mmap = snd_pcm_lib_mmap_vmalloc,
  400. };
  401. struct snd_pcm *pcm;
  402. int err;
  403. err = snd_pcm_new(isight->card, "iSight", 0, 0, 1, &pcm);
  404. if (err < 0)
  405. return err;
  406. pcm->private_data = isight;
  407. strcpy(pcm->name, "iSight");
  408. isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  409. isight->pcm->ops = &ops;
  410. return 0;
  411. }
  412. static int isight_gain_info(struct snd_kcontrol *ctl,
  413. struct snd_ctl_elem_info *info)
  414. {
  415. struct isight *isight = ctl->private_data;
  416. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  417. info->count = 1;
  418. info->value.integer.min = isight->gain_min;
  419. info->value.integer.max = isight->gain_max;
  420. return 0;
  421. }
  422. static int isight_gain_get(struct snd_kcontrol *ctl,
  423. struct snd_ctl_elem_value *value)
  424. {
  425. struct isight *isight = ctl->private_data;
  426. __be32 gain;
  427. int err;
  428. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  429. isight->audio_base + REG_GAIN, &gain, 4);
  430. if (err < 0)
  431. return err;
  432. value->value.integer.value[0] = (s32)be32_to_cpu(gain);
  433. return 0;
  434. }
  435. static int isight_gain_put(struct snd_kcontrol *ctl,
  436. struct snd_ctl_elem_value *value)
  437. {
  438. struct isight *isight = ctl->private_data;
  439. __be32 gain;
  440. if (value->value.integer.value[0] < isight->gain_min ||
  441. value->value.integer.value[0] > isight->gain_max)
  442. return -EINVAL;
  443. gain = cpu_to_be32(value->value.integer.value[0]);
  444. return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  445. isight->audio_base + REG_GAIN, &gain, 4);
  446. }
  447. static int isight_mute_get(struct snd_kcontrol *ctl,
  448. struct snd_ctl_elem_value *value)
  449. {
  450. struct isight *isight = ctl->private_data;
  451. __be32 mute;
  452. int err;
  453. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  454. isight->audio_base + REG_MUTE, &mute, 4);
  455. if (err < 0)
  456. return err;
  457. value->value.integer.value[0] = !mute;
  458. return 0;
  459. }
  460. static int isight_mute_put(struct snd_kcontrol *ctl,
  461. struct snd_ctl_elem_value *value)
  462. {
  463. struct isight *isight = ctl->private_data;
  464. __be32 mute;
  465. mute = (__force __be32)!value->value.integer.value[0];
  466. return snd_fw_transaction(isight->unit, TCODE_WRITE_QUADLET_REQUEST,
  467. isight->audio_base + REG_MUTE, &mute, 4);
  468. }
  469. static int isight_create_mixer(struct isight *isight)
  470. {
  471. static const struct snd_kcontrol_new gain_control = {
  472. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  473. .name = "Mic Capture Volume",
  474. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  475. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  476. .info = isight_gain_info,
  477. .get = isight_gain_get,
  478. .put = isight_gain_put,
  479. };
  480. static const struct snd_kcontrol_new mute_control = {
  481. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  482. .name = "Mic Capture Switch",
  483. .info = snd_ctl_boolean_mono_info,
  484. .get = isight_mute_get,
  485. .put = isight_mute_put,
  486. };
  487. __be32 value;
  488. struct snd_kcontrol *ctl;
  489. int err;
  490. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  491. isight->audio_base + REG_GAIN_RAW_START,
  492. &value, 4);
  493. if (err < 0)
  494. return err;
  495. isight->gain_min = be32_to_cpu(value);
  496. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  497. isight->audio_base + REG_GAIN_RAW_END,
  498. &value, 4);
  499. if (err < 0)
  500. return err;
  501. isight->gain_max = be32_to_cpu(value);
  502. isight->gain_tlv[0] = SNDRV_CTL_TLVT_DB_MINMAX;
  503. isight->gain_tlv[1] = 2 * sizeof(unsigned int);
  504. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  505. isight->audio_base + REG_GAIN_DB_START,
  506. &value, 4);
  507. if (err < 0)
  508. return err;
  509. isight->gain_tlv[2] = (s32)be32_to_cpu(value) * 100;
  510. err = snd_fw_transaction(isight->unit, TCODE_READ_QUADLET_REQUEST,
  511. isight->audio_base + REG_GAIN_DB_END,
  512. &value, 4);
  513. if (err < 0)
  514. return err;
  515. isight->gain_tlv[3] = (s32)be32_to_cpu(value) * 100;
  516. ctl = snd_ctl_new1(&gain_control, isight);
  517. if (ctl)
  518. ctl->tlv.p = isight->gain_tlv;
  519. err = snd_ctl_add(isight->card, ctl);
  520. if (err < 0)
  521. return err;
  522. err = snd_ctl_add(isight->card, snd_ctl_new1(&mute_control, isight));
  523. if (err < 0)
  524. return err;
  525. return 0;
  526. }
  527. static void isight_card_free(struct snd_card *card)
  528. {
  529. struct isight *isight = card->private_data;
  530. fw_iso_resources_destroy(&isight->resources);
  531. fw_unit_put(isight->unit);
  532. fw_device_put(isight->device);
  533. mutex_destroy(&isight->mutex);
  534. }
  535. static u64 get_unit_base(struct fw_unit *unit)
  536. {
  537. struct fw_csr_iterator i;
  538. int key, value;
  539. fw_csr_iterator_init(&i, unit->directory);
  540. while (fw_csr_iterator_next(&i, &key, &value))
  541. if (key == CSR_OFFSET)
  542. return CSR_REGISTER_BASE + value * 4;
  543. return 0;
  544. }
  545. static int isight_probe(struct device *unit_dev)
  546. {
  547. struct fw_unit *unit = fw_unit(unit_dev);
  548. struct fw_device *fw_dev = fw_parent_device(unit);
  549. struct snd_card *card;
  550. struct isight *isight;
  551. int err;
  552. err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*isight), &card);
  553. if (err < 0)
  554. return err;
  555. snd_card_set_dev(card, unit_dev);
  556. isight = card->private_data;
  557. isight->card = card;
  558. mutex_init(&isight->mutex);
  559. isight->unit = fw_unit_get(unit);
  560. isight->device = fw_device_get(fw_dev);
  561. isight->audio_base = get_unit_base(unit);
  562. if (!isight->audio_base) {
  563. dev_err(&unit->device, "audio unit base not found\n");
  564. err = -ENXIO;
  565. goto err_unit;
  566. }
  567. fw_iso_resources_init(&isight->resources, unit);
  568. card->private_free = isight_card_free;
  569. strcpy(card->driver, "iSight");
  570. strcpy(card->shortname, "Apple iSight");
  571. snprintf(card->longname, sizeof(card->longname),
  572. "Apple iSight (GUID %08x%08x) at %s, S%d",
  573. fw_dev->config_rom[3], fw_dev->config_rom[4],
  574. dev_name(&unit->device), 100 << fw_dev->max_speed);
  575. strcpy(card->mixername, "iSight");
  576. err = isight_create_pcm(isight);
  577. if (err < 0)
  578. goto error;
  579. err = isight_create_mixer(isight);
  580. if (err < 0)
  581. goto error;
  582. err = snd_card_register(card);
  583. if (err < 0)
  584. goto error;
  585. dev_set_drvdata(unit_dev, isight);
  586. return 0;
  587. err_unit:
  588. fw_unit_put(isight->unit);
  589. fw_device_put(isight->device);
  590. mutex_destroy(&isight->mutex);
  591. error:
  592. snd_card_free(card);
  593. return err;
  594. }
  595. static int isight_remove(struct device *dev)
  596. {
  597. struct isight *isight = dev_get_drvdata(dev);
  598. snd_card_disconnect(isight->card);
  599. mutex_lock(&isight->mutex);
  600. isight_pcm_abort(isight);
  601. isight_stop_streaming(isight);
  602. mutex_unlock(&isight->mutex);
  603. snd_card_free_when_closed(isight->card);
  604. return 0;
  605. }
  606. static void isight_bus_reset(struct fw_unit *unit)
  607. {
  608. struct isight *isight = dev_get_drvdata(&unit->device);
  609. mutex_lock(&isight->mutex);
  610. if (fw_iso_resources_update(&isight->resources) < 0) {
  611. isight_pcm_abort(isight);
  612. isight_stop_streaming(isight);
  613. }
  614. mutex_unlock(&isight->mutex);
  615. }
  616. static const struct ieee1394_device_id isight_id_table[] = {
  617. {
  618. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  619. IEEE1394_MATCH_VERSION,
  620. .specifier_id = OUI_APPLE,
  621. .version = SW_ISIGHT_AUDIO,
  622. },
  623. { }
  624. };
  625. MODULE_DEVICE_TABLE(ieee1394, isight_id_table);
  626. static struct fw_driver isight_driver = {
  627. .driver = {
  628. .owner = THIS_MODULE,
  629. .name = KBUILD_MODNAME,
  630. .bus = &fw_bus_type,
  631. .probe = isight_probe,
  632. .remove = isight_remove,
  633. },
  634. .update = isight_bus_reset,
  635. .id_table = isight_id_table,
  636. };
  637. static int __init alsa_isight_init(void)
  638. {
  639. return driver_register(&isight_driver.driver);
  640. }
  641. static void __exit alsa_isight_exit(void)
  642. {
  643. driver_unregister(&isight_driver.driver);
  644. }
  645. module_init(alsa_isight_init);
  646. module_exit(alsa_isight_exit);