cx231xx-audio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Conexant Cx231xx audio extension
  3. *
  4. * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  5. * Based on em28xx driver
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include "cx231xx.h"
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/sound.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/soundcard.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/module.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/info.h>
  32. #include <sound/initval.h>
  33. #include <sound/control.h>
  34. #include <media/v4l2-common.h>
  35. static int debug;
  36. module_param(debug, int, 0644);
  37. MODULE_PARM_DESC(debug, "activates debug info");
  38. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  39. static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
  40. {
  41. int i;
  42. dev_dbg(dev->dev, "Stopping isoc\n");
  43. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  44. if (dev->adev.urb[i]) {
  45. if (!irqs_disabled())
  46. usb_kill_urb(dev->adev.urb[i]);
  47. else
  48. usb_unlink_urb(dev->adev.urb[i]);
  49. usb_free_urb(dev->adev.urb[i]);
  50. dev->adev.urb[i] = NULL;
  51. kfree(dev->adev.transfer_buffer[i]);
  52. dev->adev.transfer_buffer[i] = NULL;
  53. }
  54. }
  55. return 0;
  56. }
  57. static int cx231xx_bulk_audio_deinit(struct cx231xx *dev)
  58. {
  59. int i;
  60. dev_dbg(dev->dev, "Stopping bulk\n");
  61. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  62. if (dev->adev.urb[i]) {
  63. if (!irqs_disabled())
  64. usb_kill_urb(dev->adev.urb[i]);
  65. else
  66. usb_unlink_urb(dev->adev.urb[i]);
  67. usb_free_urb(dev->adev.urb[i]);
  68. dev->adev.urb[i] = NULL;
  69. kfree(dev->adev.transfer_buffer[i]);
  70. dev->adev.transfer_buffer[i] = NULL;
  71. }
  72. }
  73. return 0;
  74. }
  75. static void cx231xx_audio_isocirq(struct urb *urb)
  76. {
  77. struct cx231xx *dev = urb->context;
  78. int i;
  79. unsigned int oldptr;
  80. int period_elapsed = 0;
  81. int status;
  82. unsigned char *cp;
  83. unsigned int stride;
  84. struct snd_pcm_substream *substream;
  85. struct snd_pcm_runtime *runtime;
  86. if (dev->state & DEV_DISCONNECTED)
  87. return;
  88. switch (urb->status) {
  89. case 0: /* success */
  90. case -ETIMEDOUT: /* NAK */
  91. break;
  92. case -ECONNRESET: /* kill */
  93. case -ENOENT:
  94. case -ESHUTDOWN:
  95. return;
  96. default: /* error */
  97. dev_dbg(dev->dev, "urb completion error %d.\n",
  98. urb->status);
  99. break;
  100. }
  101. if (atomic_read(&dev->stream_started) == 0)
  102. return;
  103. if (dev->adev.capture_pcm_substream) {
  104. substream = dev->adev.capture_pcm_substream;
  105. runtime = substream->runtime;
  106. stride = runtime->frame_bits >> 3;
  107. for (i = 0; i < urb->number_of_packets; i++) {
  108. unsigned long flags;
  109. int length = urb->iso_frame_desc[i].actual_length /
  110. stride;
  111. cp = (unsigned char *)urb->transfer_buffer +
  112. urb->iso_frame_desc[i].offset;
  113. if (!length)
  114. continue;
  115. oldptr = dev->adev.hwptr_done_capture;
  116. if (oldptr + length >= runtime->buffer_size) {
  117. unsigned int cnt;
  118. cnt = runtime->buffer_size - oldptr;
  119. memcpy(runtime->dma_area + oldptr * stride, cp,
  120. cnt * stride);
  121. memcpy(runtime->dma_area, cp + cnt * stride,
  122. length * stride - cnt * stride);
  123. } else {
  124. memcpy(runtime->dma_area + oldptr * stride, cp,
  125. length * stride);
  126. }
  127. snd_pcm_stream_lock_irqsave(substream, flags);
  128. dev->adev.hwptr_done_capture += length;
  129. if (dev->adev.hwptr_done_capture >=
  130. runtime->buffer_size)
  131. dev->adev.hwptr_done_capture -=
  132. runtime->buffer_size;
  133. dev->adev.capture_transfer_done += length;
  134. if (dev->adev.capture_transfer_done >=
  135. runtime->period_size) {
  136. dev->adev.capture_transfer_done -=
  137. runtime->period_size;
  138. period_elapsed = 1;
  139. }
  140. snd_pcm_stream_unlock_irqrestore(substream, flags);
  141. }
  142. if (period_elapsed)
  143. snd_pcm_period_elapsed(substream);
  144. }
  145. urb->status = 0;
  146. status = usb_submit_urb(urb, GFP_ATOMIC);
  147. if (status < 0) {
  148. dev_err(dev->dev,
  149. "resubmit of audio urb failed (error=%i)\n",
  150. status);
  151. }
  152. return;
  153. }
  154. static void cx231xx_audio_bulkirq(struct urb *urb)
  155. {
  156. struct cx231xx *dev = urb->context;
  157. unsigned int oldptr;
  158. int period_elapsed = 0;
  159. int status;
  160. unsigned char *cp;
  161. unsigned int stride;
  162. struct snd_pcm_substream *substream;
  163. struct snd_pcm_runtime *runtime;
  164. if (dev->state & DEV_DISCONNECTED)
  165. return;
  166. switch (urb->status) {
  167. case 0: /* success */
  168. case -ETIMEDOUT: /* NAK */
  169. break;
  170. case -ECONNRESET: /* kill */
  171. case -ENOENT:
  172. case -ESHUTDOWN:
  173. return;
  174. default: /* error */
  175. dev_dbg(dev->dev, "urb completion error %d.\n",
  176. urb->status);
  177. break;
  178. }
  179. if (atomic_read(&dev->stream_started) == 0)
  180. return;
  181. if (dev->adev.capture_pcm_substream) {
  182. substream = dev->adev.capture_pcm_substream;
  183. runtime = substream->runtime;
  184. stride = runtime->frame_bits >> 3;
  185. if (1) {
  186. unsigned long flags;
  187. int length = urb->actual_length /
  188. stride;
  189. cp = (unsigned char *)urb->transfer_buffer;
  190. oldptr = dev->adev.hwptr_done_capture;
  191. if (oldptr + length >= runtime->buffer_size) {
  192. unsigned int cnt;
  193. cnt = runtime->buffer_size - oldptr;
  194. memcpy(runtime->dma_area + oldptr * stride, cp,
  195. cnt * stride);
  196. memcpy(runtime->dma_area, cp + cnt * stride,
  197. length * stride - cnt * stride);
  198. } else {
  199. memcpy(runtime->dma_area + oldptr * stride, cp,
  200. length * stride);
  201. }
  202. snd_pcm_stream_lock_irqsave(substream, flags);
  203. dev->adev.hwptr_done_capture += length;
  204. if (dev->adev.hwptr_done_capture >=
  205. runtime->buffer_size)
  206. dev->adev.hwptr_done_capture -=
  207. runtime->buffer_size;
  208. dev->adev.capture_transfer_done += length;
  209. if (dev->adev.capture_transfer_done >=
  210. runtime->period_size) {
  211. dev->adev.capture_transfer_done -=
  212. runtime->period_size;
  213. period_elapsed = 1;
  214. }
  215. snd_pcm_stream_unlock_irqrestore(substream, flags);
  216. }
  217. if (period_elapsed)
  218. snd_pcm_period_elapsed(substream);
  219. }
  220. urb->status = 0;
  221. status = usb_submit_urb(urb, GFP_ATOMIC);
  222. if (status < 0) {
  223. dev_err(dev->dev,
  224. "resubmit of audio urb failed (error=%i)\n",
  225. status);
  226. }
  227. return;
  228. }
  229. static int cx231xx_init_audio_isoc(struct cx231xx *dev)
  230. {
  231. int i, errCode;
  232. int sb_size;
  233. dev_dbg(dev->dev,
  234. "%s: Starting ISO AUDIO transfers\n", __func__);
  235. if (dev->state & DEV_DISCONNECTED)
  236. return -ENODEV;
  237. sb_size = CX231XX_ISO_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
  238. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  239. struct urb *urb;
  240. int j, k;
  241. dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  242. if (!dev->adev.transfer_buffer[i])
  243. return -ENOMEM;
  244. memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
  245. urb = usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  246. if (!urb) {
  247. for (j = 0; j < i; j++) {
  248. usb_free_urb(dev->adev.urb[j]);
  249. kfree(dev->adev.transfer_buffer[j]);
  250. }
  251. return -ENOMEM;
  252. }
  253. urb->dev = dev->udev;
  254. urb->context = dev;
  255. urb->pipe = usb_rcvisocpipe(dev->udev,
  256. dev->adev.end_point_addr);
  257. urb->transfer_flags = URB_ISO_ASAP;
  258. urb->transfer_buffer = dev->adev.transfer_buffer[i];
  259. urb->interval = 1;
  260. urb->complete = cx231xx_audio_isocirq;
  261. urb->number_of_packets = CX231XX_ISO_NUM_AUDIO_PACKETS;
  262. urb->transfer_buffer_length = sb_size;
  263. for (j = k = 0; j < CX231XX_ISO_NUM_AUDIO_PACKETS;
  264. j++, k += dev->adev.max_pkt_size) {
  265. urb->iso_frame_desc[j].offset = k;
  266. urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
  267. }
  268. dev->adev.urb[i] = urb;
  269. }
  270. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  271. errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  272. if (errCode < 0) {
  273. cx231xx_isoc_audio_deinit(dev);
  274. return errCode;
  275. }
  276. }
  277. return errCode;
  278. }
  279. static int cx231xx_init_audio_bulk(struct cx231xx *dev)
  280. {
  281. int i, errCode;
  282. int sb_size;
  283. dev_dbg(dev->dev,
  284. "%s: Starting BULK AUDIO transfers\n", __func__);
  285. if (dev->state & DEV_DISCONNECTED)
  286. return -ENODEV;
  287. sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
  288. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  289. struct urb *urb;
  290. int j;
  291. dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
  292. if (!dev->adev.transfer_buffer[i])
  293. return -ENOMEM;
  294. memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
  295. urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
  296. if (!urb) {
  297. for (j = 0; j < i; j++) {
  298. usb_free_urb(dev->adev.urb[j]);
  299. kfree(dev->adev.transfer_buffer[j]);
  300. }
  301. return -ENOMEM;
  302. }
  303. urb->dev = dev->udev;
  304. urb->context = dev;
  305. urb->pipe = usb_rcvbulkpipe(dev->udev,
  306. dev->adev.end_point_addr);
  307. urb->transfer_flags = 0;
  308. urb->transfer_buffer = dev->adev.transfer_buffer[i];
  309. urb->complete = cx231xx_audio_bulkirq;
  310. urb->transfer_buffer_length = sb_size;
  311. dev->adev.urb[i] = urb;
  312. }
  313. for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  314. errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  315. if (errCode < 0) {
  316. cx231xx_bulk_audio_deinit(dev);
  317. return errCode;
  318. }
  319. }
  320. return errCode;
  321. }
  322. static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
  323. size_t size)
  324. {
  325. struct snd_pcm_runtime *runtime = subs->runtime;
  326. struct cx231xx *dev = snd_pcm_substream_chip(subs);
  327. dev_dbg(dev->dev, "Allocating vbuffer\n");
  328. if (runtime->dma_area) {
  329. if (runtime->dma_bytes > size)
  330. return 0;
  331. vfree(runtime->dma_area);
  332. }
  333. runtime->dma_area = vmalloc(size);
  334. if (!runtime->dma_area)
  335. return -ENOMEM;
  336. runtime->dma_bytes = size;
  337. return 0;
  338. }
  339. static const struct snd_pcm_hardware snd_cx231xx_hw_capture = {
  340. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  341. SNDRV_PCM_INFO_MMAP |
  342. SNDRV_PCM_INFO_INTERLEAVED |
  343. SNDRV_PCM_INFO_MMAP_VALID,
  344. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  345. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  346. .rate_min = 48000,
  347. .rate_max = 48000,
  348. .channels_min = 2,
  349. .channels_max = 2,
  350. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  351. .period_bytes_min = 64, /* 12544/2, */
  352. .period_bytes_max = 12544,
  353. .periods_min = 2,
  354. .periods_max = 98, /* 12544, */
  355. };
  356. static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
  357. {
  358. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  359. struct snd_pcm_runtime *runtime = substream->runtime;
  360. int ret = 0;
  361. dev_dbg(dev->dev,
  362. "opening device and trying to acquire exclusive lock\n");
  363. if (dev->state & DEV_DISCONNECTED) {
  364. dev_err(dev->dev,
  365. "Can't open. the device was removed.\n");
  366. return -ENODEV;
  367. }
  368. /* set alternate setting for audio interface */
  369. /* 1 - 48000 samples per sec */
  370. mutex_lock(&dev->lock);
  371. if (dev->USE_ISO)
  372. ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1);
  373. else
  374. ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
  375. mutex_unlock(&dev->lock);
  376. if (ret < 0) {
  377. dev_err(dev->dev,
  378. "failed to set alternate setting !\n");
  379. return ret;
  380. }
  381. runtime->hw = snd_cx231xx_hw_capture;
  382. mutex_lock(&dev->lock);
  383. /* inform hardware to start streaming */
  384. ret = cx231xx_capture_start(dev, 1, Audio);
  385. dev->adev.users++;
  386. mutex_unlock(&dev->lock);
  387. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  388. dev->adev.capture_pcm_substream = substream;
  389. runtime->private_data = dev;
  390. return 0;
  391. }
  392. static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
  393. {
  394. int ret;
  395. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  396. dev_dbg(dev->dev, "closing device\n");
  397. /* inform hardware to stop streaming */
  398. mutex_lock(&dev->lock);
  399. ret = cx231xx_capture_start(dev, 0, Audio);
  400. /* set alternate setting for audio interface */
  401. /* 1 - 48000 samples per sec */
  402. ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
  403. if (ret < 0) {
  404. dev_err(dev->dev,
  405. "failed to set alternate setting !\n");
  406. mutex_unlock(&dev->lock);
  407. return ret;
  408. }
  409. dev->adev.users--;
  410. if (substream->runtime->dma_area) {
  411. dev_dbg(dev->dev, "freeing\n");
  412. vfree(substream->runtime->dma_area);
  413. substream->runtime->dma_area = NULL;
  414. }
  415. mutex_unlock(&dev->lock);
  416. if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
  417. dev_dbg(dev->dev, "audio users: %d\n", dev->adev.users);
  418. dev_dbg(dev->dev, "disabling audio stream!\n");
  419. dev->adev.shutdown = 0;
  420. dev_dbg(dev->dev, "released lock\n");
  421. if (atomic_read(&dev->stream_started) > 0) {
  422. atomic_set(&dev->stream_started, 0);
  423. schedule_work(&dev->wq_trigger);
  424. }
  425. }
  426. return 0;
  427. }
  428. static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
  429. struct snd_pcm_hw_params *hw_params)
  430. {
  431. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  432. int ret;
  433. dev_dbg(dev->dev, "Setting capture parameters\n");
  434. ret = snd_pcm_alloc_vmalloc_buffer(substream,
  435. params_buffer_bytes(hw_params));
  436. #if 0
  437. /* TODO: set up cx231xx audio chip to deliver the correct audio format,
  438. current default is 48000hz multiplexed => 96000hz mono
  439. which shouldn't matter since analogue TV only supports mono */
  440. unsigned int channels, rate, format;
  441. format = params_format(hw_params);
  442. rate = params_rate(hw_params);
  443. channels = params_channels(hw_params);
  444. #endif
  445. return ret;
  446. }
  447. static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
  448. {
  449. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  450. dev_dbg(dev->dev, "Stop capture, if needed\n");
  451. if (atomic_read(&dev->stream_started) > 0) {
  452. atomic_set(&dev->stream_started, 0);
  453. schedule_work(&dev->wq_trigger);
  454. }
  455. return 0;
  456. }
  457. static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
  458. {
  459. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  460. dev->adev.hwptr_done_capture = 0;
  461. dev->adev.capture_transfer_done = 0;
  462. return 0;
  463. }
  464. static void audio_trigger(struct work_struct *work)
  465. {
  466. struct cx231xx *dev = container_of(work, struct cx231xx, wq_trigger);
  467. if (atomic_read(&dev->stream_started)) {
  468. dev_dbg(dev->dev, "starting capture");
  469. if (is_fw_load(dev) == 0)
  470. cx25840_call(dev, core, load_fw);
  471. if (dev->USE_ISO)
  472. cx231xx_init_audio_isoc(dev);
  473. else
  474. cx231xx_init_audio_bulk(dev);
  475. } else {
  476. dev_dbg(dev->dev, "stopping capture");
  477. cx231xx_isoc_audio_deinit(dev);
  478. }
  479. }
  480. static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
  481. int cmd)
  482. {
  483. struct cx231xx *dev = snd_pcm_substream_chip(substream);
  484. int retval = 0;
  485. if (dev->state & DEV_DISCONNECTED)
  486. return -ENODEV;
  487. spin_lock(&dev->adev.slock);
  488. switch (cmd) {
  489. case SNDRV_PCM_TRIGGER_START:
  490. atomic_set(&dev->stream_started, 1);
  491. break;
  492. case SNDRV_PCM_TRIGGER_STOP:
  493. atomic_set(&dev->stream_started, 0);
  494. break;
  495. default:
  496. retval = -EINVAL;
  497. break;
  498. }
  499. spin_unlock(&dev->adev.slock);
  500. schedule_work(&dev->wq_trigger);
  501. return retval;
  502. }
  503. static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
  504. *substream)
  505. {
  506. struct cx231xx *dev;
  507. unsigned long flags;
  508. snd_pcm_uframes_t hwptr_done;
  509. dev = snd_pcm_substream_chip(substream);
  510. spin_lock_irqsave(&dev->adev.slock, flags);
  511. hwptr_done = dev->adev.hwptr_done_capture;
  512. spin_unlock_irqrestore(&dev->adev.slock, flags);
  513. return hwptr_done;
  514. }
  515. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  516. unsigned long offset)
  517. {
  518. void *pageptr = subs->runtime->dma_area + offset;
  519. return vmalloc_to_page(pageptr);
  520. }
  521. static const struct snd_pcm_ops snd_cx231xx_pcm_capture = {
  522. .open = snd_cx231xx_capture_open,
  523. .close = snd_cx231xx_pcm_close,
  524. .ioctl = snd_pcm_lib_ioctl,
  525. .hw_params = snd_cx231xx_hw_capture_params,
  526. .hw_free = snd_cx231xx_hw_capture_free,
  527. .prepare = snd_cx231xx_prepare,
  528. .trigger = snd_cx231xx_capture_trigger,
  529. .pointer = snd_cx231xx_capture_pointer,
  530. .page = snd_pcm_get_vmalloc_page,
  531. };
  532. static int cx231xx_audio_init(struct cx231xx *dev)
  533. {
  534. struct cx231xx_audio *adev = &dev->adev;
  535. struct snd_pcm *pcm;
  536. struct snd_card *card;
  537. static int devnr;
  538. int err;
  539. struct usb_interface *uif;
  540. int i, isoc_pipe = 0;
  541. if (dev->has_alsa_audio != 1) {
  542. /* This device does not support the extension (in this case
  543. the device is expecting the snd-usb-audio module or
  544. doesn't have analog audio support at all) */
  545. return 0;
  546. }
  547. dev_dbg(dev->dev,
  548. "probing for cx231xx non standard usbaudio\n");
  549. err = snd_card_new(dev->dev, index[devnr], "Cx231xx Audio",
  550. THIS_MODULE, 0, &card);
  551. if (err < 0)
  552. return err;
  553. spin_lock_init(&adev->slock);
  554. err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
  555. if (err < 0)
  556. goto err_free_card;
  557. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  558. &snd_cx231xx_pcm_capture);
  559. pcm->info_flags = 0;
  560. pcm->private_data = dev;
  561. strcpy(pcm->name, "Conexant cx231xx Capture");
  562. strcpy(card->driver, "Cx231xx-Audio");
  563. strcpy(card->shortname, "Cx231xx Audio");
  564. strcpy(card->longname, "Conexant cx231xx Audio");
  565. INIT_WORK(&dev->wq_trigger, audio_trigger);
  566. err = snd_card_register(card);
  567. if (err < 0)
  568. goto err_free_card;
  569. adev->sndcard = card;
  570. adev->udev = dev->udev;
  571. /* compute alternate max packet sizes for Audio */
  572. uif =
  573. dev->udev->actconfig->interface[dev->current_pcb_config.
  574. hs_config_info[0].interface_info.
  575. audio_index + 1];
  576. if (uif->altsetting[0].desc.bNumEndpoints < isoc_pipe + 1) {
  577. err = -ENODEV;
  578. goto err_free_card;
  579. }
  580. adev->end_point_addr =
  581. uif->altsetting[0].endpoint[isoc_pipe].desc.
  582. bEndpointAddress;
  583. adev->num_alt = uif->num_altsetting;
  584. dev_info(dev->dev,
  585. "audio EndPoint Addr 0x%x, Alternate settings: %i\n",
  586. adev->end_point_addr, adev->num_alt);
  587. adev->alt_max_pkt_size = kmalloc_array(32, adev->num_alt, GFP_KERNEL);
  588. if (!adev->alt_max_pkt_size) {
  589. err = -ENOMEM;
  590. goto err_free_card;
  591. }
  592. for (i = 0; i < adev->num_alt; i++) {
  593. u16 tmp;
  594. if (uif->altsetting[i].desc.bNumEndpoints < isoc_pipe + 1) {
  595. err = -ENODEV;
  596. goto err_free_pkt_size;
  597. }
  598. tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
  599. wMaxPacketSize);
  600. adev->alt_max_pkt_size[i] =
  601. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  602. dev_dbg(dev->dev,
  603. "audio alternate setting %i, max size= %i\n", i,
  604. adev->alt_max_pkt_size[i]);
  605. }
  606. return 0;
  607. err_free_pkt_size:
  608. kfree(adev->alt_max_pkt_size);
  609. err_free_card:
  610. snd_card_free(card);
  611. return err;
  612. }
  613. static int cx231xx_audio_fini(struct cx231xx *dev)
  614. {
  615. if (dev == NULL)
  616. return 0;
  617. if (dev->has_alsa_audio != 1) {
  618. /* This device does not support the extension (in this case
  619. the device is expecting the snd-usb-audio module or
  620. doesn't have analog audio support at all) */
  621. return 0;
  622. }
  623. if (dev->adev.sndcard) {
  624. snd_card_free(dev->adev.sndcard);
  625. kfree(dev->adev.alt_max_pkt_size);
  626. dev->adev.sndcard = NULL;
  627. }
  628. return 0;
  629. }
  630. static struct cx231xx_ops audio_ops = {
  631. .id = CX231XX_AUDIO,
  632. .name = "Cx231xx Audio Extension",
  633. .init = cx231xx_audio_init,
  634. .fini = cx231xx_audio_fini,
  635. };
  636. static int __init cx231xx_alsa_register(void)
  637. {
  638. return cx231xx_register_extension(&audio_ops);
  639. }
  640. static void __exit cx231xx_alsa_unregister(void)
  641. {
  642. cx231xx_unregister_extension(&audio_ops);
  643. }
  644. MODULE_LICENSE("GPL");
  645. MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
  646. MODULE_DESCRIPTION("Cx231xx Audio driver");
  647. module_init(cx231xx_alsa_register);
  648. module_exit(cx231xx_alsa_unregister);