cx231xx-audio.c 19 KB

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