/drivers/staging/line6/pcm.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 522 lines · 372 code · 86 blank · 64 comment · 45 complexity · a05c75cca623ba5cf5d962c5d13be9c2 MD5 · raw file

  1. /*
  2. * Line6 Linux USB driver - 0.9.1beta
  3. *
  4. * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <sound/core.h>
  13. #include <sound/control.h>
  14. #include <sound/pcm.h>
  15. #include <sound/pcm_params.h>
  16. #include "audio.h"
  17. #include "capture.h"
  18. #include "driver.h"
  19. #include "playback.h"
  20. #include "pod.h"
  21. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  22. static struct snd_line6_pcm *dev2pcm(struct device *dev)
  23. {
  24. struct usb_interface *interface = to_usb_interface(dev);
  25. struct usb_line6 *line6 = usb_get_intfdata(interface);
  26. struct snd_line6_pcm *line6pcm = line6->line6pcm;
  27. return line6pcm;
  28. }
  29. /*
  30. "read" request on "impulse_volume" special file.
  31. */
  32. static ssize_t pcm_get_impulse_volume(struct device *dev,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_volume);
  36. }
  37. /*
  38. "write" request on "impulse_volume" special file.
  39. */
  40. static ssize_t pcm_set_impulse_volume(struct device *dev,
  41. struct device_attribute *attr,
  42. const char *buf, size_t count)
  43. {
  44. struct snd_line6_pcm *line6pcm = dev2pcm(dev);
  45. int value = simple_strtoul(buf, NULL, 10);
  46. line6pcm->impulse_volume = value;
  47. if (value > 0)
  48. line6_pcm_start(line6pcm, MASK_PCM_IMPULSE);
  49. else
  50. line6_pcm_stop(line6pcm, MASK_PCM_IMPULSE);
  51. return count;
  52. }
  53. /*
  54. "read" request on "impulse_period" special file.
  55. */
  56. static ssize_t pcm_get_impulse_period(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
  60. }
  61. /*
  62. "write" request on "impulse_period" special file.
  63. */
  64. static ssize_t pcm_set_impulse_period(struct device *dev,
  65. struct device_attribute *attr,
  66. const char *buf, size_t count)
  67. {
  68. dev2pcm(dev)->impulse_period = simple_strtoul(buf, NULL, 10);
  69. return count;
  70. }
  71. static DEVICE_ATTR(impulse_volume, S_IWUSR | S_IRUGO, pcm_get_impulse_volume,
  72. pcm_set_impulse_volume);
  73. static DEVICE_ATTR(impulse_period, S_IWUSR | S_IRUGO, pcm_get_impulse_period,
  74. pcm_set_impulse_period);
  75. #endif
  76. int line6_pcm_start(struct snd_line6_pcm *line6pcm, int channels)
  77. {
  78. unsigned long flags_old =
  79. __sync_fetch_and_or(&line6pcm->flags, channels);
  80. unsigned long flags_new = flags_old | channels;
  81. int err = 0;
  82. #if LINE6_BACKUP_MONITOR_SIGNAL
  83. if (!(line6pcm->line6->properties->capabilities & LINE6_BIT_HWMON)) {
  84. line6pcm->prev_fbuf =
  85. kmalloc(LINE6_ISO_PACKETS * line6pcm->max_packet_size,
  86. GFP_KERNEL);
  87. if (!line6pcm->prev_fbuf) {
  88. dev_err(line6pcm->line6->ifcdev,
  89. "cannot malloc monitor buffer\n");
  90. return -ENOMEM;
  91. }
  92. }
  93. #else
  94. line6pcm->prev_fbuf = NULL;
  95. #endif
  96. if (((flags_old & MASK_CAPTURE) == 0) &&
  97. ((flags_new & MASK_CAPTURE) != 0)) {
  98. /*
  99. Waiting for completion of active URBs in the stop handler is
  100. a bug, we therefore report an error if capturing is restarted
  101. too soon.
  102. */
  103. if (line6pcm->active_urb_in | line6pcm->unlink_urb_in)
  104. return -EBUSY;
  105. line6pcm->buffer_in =
  106. kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
  107. line6pcm->max_packet_size, GFP_KERNEL);
  108. if (!line6pcm->buffer_in) {
  109. dev_err(line6pcm->line6->ifcdev,
  110. "cannot malloc capture buffer\n");
  111. return -ENOMEM;
  112. }
  113. line6pcm->count_in = 0;
  114. line6pcm->prev_fsize = 0;
  115. err = line6_submit_audio_in_all_urbs(line6pcm);
  116. if (err < 0) {
  117. __sync_fetch_and_and(&line6pcm->flags, ~channels);
  118. return err;
  119. }
  120. }
  121. if (((flags_old & MASK_PLAYBACK) == 0) &&
  122. ((flags_new & MASK_PLAYBACK) != 0)) {
  123. /*
  124. See comment above regarding PCM restart.
  125. */
  126. if (line6pcm->active_urb_out | line6pcm->unlink_urb_out)
  127. return -EBUSY;
  128. line6pcm->buffer_out =
  129. kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
  130. line6pcm->max_packet_size, GFP_KERNEL);
  131. if (!line6pcm->buffer_out) {
  132. dev_err(line6pcm->line6->ifcdev,
  133. "cannot malloc playback buffer\n");
  134. return -ENOMEM;
  135. }
  136. line6pcm->count_out = 0;
  137. err = line6_submit_audio_out_all_urbs(line6pcm);
  138. if (err < 0) {
  139. __sync_fetch_and_and(&line6pcm->flags, ~channels);
  140. return err;
  141. }
  142. }
  143. return 0;
  144. }
  145. int line6_pcm_stop(struct snd_line6_pcm *line6pcm, int channels)
  146. {
  147. unsigned long flags_old =
  148. __sync_fetch_and_and(&line6pcm->flags, ~channels);
  149. unsigned long flags_new = flags_old & ~channels;
  150. if (((flags_old & MASK_CAPTURE) != 0) &&
  151. ((flags_new & MASK_CAPTURE) == 0)) {
  152. line6_unlink_audio_in_urbs(line6pcm);
  153. kfree(line6pcm->buffer_in);
  154. line6pcm->buffer_in = NULL;
  155. }
  156. if (((flags_old & MASK_PLAYBACK) != 0) &&
  157. ((flags_new & MASK_PLAYBACK) == 0)) {
  158. line6_unlink_audio_out_urbs(line6pcm);
  159. kfree(line6pcm->buffer_out);
  160. line6pcm->buffer_out = NULL;
  161. }
  162. #if LINE6_BACKUP_MONITOR_SIGNAL
  163. kfree(line6pcm->prev_fbuf);
  164. #endif
  165. return 0;
  166. }
  167. /* trigger callback */
  168. int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
  169. {
  170. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  171. struct snd_pcm_substream *s;
  172. int err;
  173. unsigned long flags;
  174. spin_lock_irqsave(&line6pcm->lock_trigger, flags);
  175. clear_bit(BIT_PREPARED, &line6pcm->flags);
  176. snd_pcm_group_for_each_entry(s, substream) {
  177. switch (s->stream) {
  178. case SNDRV_PCM_STREAM_PLAYBACK:
  179. err = snd_line6_playback_trigger(line6pcm, cmd);
  180. if (err < 0) {
  181. spin_unlock_irqrestore(&line6pcm->lock_trigger,
  182. flags);
  183. return err;
  184. }
  185. break;
  186. case SNDRV_PCM_STREAM_CAPTURE:
  187. err = snd_line6_capture_trigger(line6pcm, cmd);
  188. if (err < 0) {
  189. spin_unlock_irqrestore(&line6pcm->lock_trigger,
  190. flags);
  191. return err;
  192. }
  193. break;
  194. default:
  195. dev_err(line6pcm->line6->ifcdev,
  196. "Unknown stream direction %d\n", s->stream);
  197. }
  198. }
  199. spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
  200. return 0;
  201. }
  202. /* control info callback */
  203. static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
  204. struct snd_ctl_elem_info *uinfo)
  205. {
  206. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  207. uinfo->count = 2;
  208. uinfo->value.integer.min = 0;
  209. uinfo->value.integer.max = 256;
  210. return 0;
  211. }
  212. /* control get callback */
  213. static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
  214. struct snd_ctl_elem_value *ucontrol)
  215. {
  216. int i;
  217. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  218. for (i = 2; i--;)
  219. ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
  220. return 0;
  221. }
  222. /* control put callback */
  223. static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
  224. struct snd_ctl_elem_value *ucontrol)
  225. {
  226. int i, changed = 0;
  227. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  228. for (i = 2; i--;)
  229. if (line6pcm->volume_playback[i] !=
  230. ucontrol->value.integer.value[i]) {
  231. line6pcm->volume_playback[i] =
  232. ucontrol->value.integer.value[i];
  233. changed = 1;
  234. }
  235. return changed;
  236. }
  237. /* control definition */
  238. static struct snd_kcontrol_new line6_control_playback = {
  239. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  240. .name = "PCM Playback Volume",
  241. .index = 0,
  242. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  243. .info = snd_line6_control_playback_info,
  244. .get = snd_line6_control_playback_get,
  245. .put = snd_line6_control_playback_put
  246. };
  247. /*
  248. Cleanup the PCM device.
  249. */
  250. static void line6_cleanup_pcm(struct snd_pcm *pcm)
  251. {
  252. int i;
  253. struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
  254. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  255. device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
  256. device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
  257. #endif
  258. for (i = LINE6_ISO_BUFFERS; i--;) {
  259. if (line6pcm->urb_audio_out[i]) {
  260. usb_kill_urb(line6pcm->urb_audio_out[i]);
  261. usb_free_urb(line6pcm->urb_audio_out[i]);
  262. }
  263. if (line6pcm->urb_audio_in[i]) {
  264. usb_kill_urb(line6pcm->urb_audio_in[i]);
  265. usb_free_urb(line6pcm->urb_audio_in[i]);
  266. }
  267. }
  268. }
  269. /* create a PCM device */
  270. static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
  271. {
  272. struct snd_pcm *pcm;
  273. int err;
  274. err = snd_pcm_new(line6pcm->line6->card,
  275. (char *)line6pcm->line6->properties->name,
  276. 0, 1, 1, &pcm);
  277. if (err < 0)
  278. return err;
  279. pcm->private_data = line6pcm;
  280. pcm->private_free = line6_cleanup_pcm;
  281. line6pcm->pcm = pcm;
  282. strcpy(pcm->name, line6pcm->line6->properties->name);
  283. /* set operators */
  284. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  285. &snd_line6_playback_ops);
  286. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
  287. /* pre-allocation of buffers */
  288. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  289. snd_dma_continuous_data
  290. (GFP_KERNEL), 64 * 1024,
  291. 128 * 1024);
  292. return 0;
  293. }
  294. /* PCM device destructor */
  295. static int snd_line6_pcm_free(struct snd_device *device)
  296. {
  297. return 0;
  298. }
  299. /*
  300. Stop substream if still running.
  301. */
  302. static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
  303. {
  304. if (substream->runtime && snd_pcm_running(substream))
  305. snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
  306. }
  307. /*
  308. Stop PCM stream.
  309. */
  310. void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
  311. {
  312. pcm_disconnect_substream(get_substream
  313. (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
  314. pcm_disconnect_substream(get_substream
  315. (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
  316. line6_unlink_wait_clear_audio_out_urbs(line6pcm);
  317. line6_unlink_wait_clear_audio_in_urbs(line6pcm);
  318. }
  319. /*
  320. Create and register the PCM device and mixer entries.
  321. Create URBs for playback and capture.
  322. */
  323. int line6_init_pcm(struct usb_line6 *line6,
  324. struct line6_pcm_properties *properties)
  325. {
  326. static struct snd_device_ops pcm_ops = {
  327. .dev_free = snd_line6_pcm_free,
  328. };
  329. int err;
  330. int ep_read = 0, ep_write = 0;
  331. struct snd_line6_pcm *line6pcm;
  332. if (!(line6->properties->capabilities & LINE6_BIT_PCM))
  333. return 0; /* skip PCM initialization and report success */
  334. /* initialize PCM subsystem based on product id: */
  335. switch (line6->product) {
  336. case LINE6_DEVID_BASSPODXT:
  337. case LINE6_DEVID_BASSPODXTLIVE:
  338. case LINE6_DEVID_BASSPODXTPRO:
  339. case LINE6_DEVID_PODXT:
  340. case LINE6_DEVID_PODXTLIVE:
  341. case LINE6_DEVID_PODXTPRO:
  342. ep_read = 0x82;
  343. ep_write = 0x01;
  344. break;
  345. case LINE6_DEVID_PODX3:
  346. case LINE6_DEVID_PODX3LIVE:
  347. ep_read = 0x86;
  348. ep_write = 0x02;
  349. break;
  350. case LINE6_DEVID_POCKETPOD:
  351. ep_read = 0x82;
  352. ep_write = 0x02;
  353. break;
  354. case LINE6_DEVID_GUITARPORT:
  355. case LINE6_DEVID_PODSTUDIO_GX:
  356. case LINE6_DEVID_PODSTUDIO_UX1:
  357. case LINE6_DEVID_PODSTUDIO_UX2:
  358. case LINE6_DEVID_TONEPORT_GX:
  359. case LINE6_DEVID_TONEPORT_UX1:
  360. case LINE6_DEVID_TONEPORT_UX2:
  361. ep_read = 0x82;
  362. ep_write = 0x01;
  363. break;
  364. /* this is for interface_number == 1:
  365. case LINE6_DEVID_TONEPORT_UX2:
  366. case LINE6_DEVID_PODSTUDIO_UX2:
  367. ep_read = 0x87;
  368. ep_write = 0x00;
  369. break;
  370. */
  371. default:
  372. MISSING_CASE;
  373. }
  374. line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
  375. if (line6pcm == NULL)
  376. return -ENOMEM;
  377. line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
  378. line6pcm->volume_monitor = 255;
  379. line6pcm->line6 = line6;
  380. line6pcm->ep_audio_read = ep_read;
  381. line6pcm->ep_audio_write = ep_write;
  382. line6pcm->max_packet_size = usb_maxpacket(line6->usbdev,
  383. usb_rcvintpipe(line6->usbdev,
  384. ep_read), 0);
  385. line6pcm->properties = properties;
  386. line6->line6pcm = line6pcm;
  387. /* PCM device: */
  388. err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
  389. if (err < 0)
  390. return err;
  391. snd_card_set_dev(line6->card, line6->ifcdev);
  392. err = snd_line6_new_pcm(line6pcm);
  393. if (err < 0)
  394. return err;
  395. spin_lock_init(&line6pcm->lock_audio_out);
  396. spin_lock_init(&line6pcm->lock_audio_in);
  397. spin_lock_init(&line6pcm->lock_trigger);
  398. err = line6_create_audio_out_urbs(line6pcm);
  399. if (err < 0)
  400. return err;
  401. err = line6_create_audio_in_urbs(line6pcm);
  402. if (err < 0)
  403. return err;
  404. /* mixer: */
  405. err =
  406. snd_ctl_add(line6->card,
  407. snd_ctl_new1(&line6_control_playback, line6pcm));
  408. if (err < 0)
  409. return err;
  410. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  411. /* impulse response test: */
  412. err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
  413. if (err < 0)
  414. return err;
  415. err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
  416. if (err < 0)
  417. return err;
  418. line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
  419. #endif
  420. return 0;
  421. }
  422. /* prepare pcm callback */
  423. int snd_line6_prepare(struct snd_pcm_substream *substream)
  424. {
  425. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  426. if (!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
  427. line6pcm->count_out = 0;
  428. line6pcm->pos_out = 0;
  429. line6pcm->pos_out_done = 0;
  430. line6pcm->bytes_out = 0;
  431. line6pcm->count_in = 0;
  432. line6pcm->pos_in_done = 0;
  433. line6pcm->bytes_in = 0;
  434. }
  435. return 0;
  436. }