pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 impulse_volume_show(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 impulse_volume_store(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;
  46. int ret;
  47. ret = kstrtoint(buf, 10, &value);
  48. if (ret < 0)
  49. return ret;
  50. line6pcm->impulse_volume = value;
  51. if (value > 0)
  52. line6_pcm_acquire(line6pcm, LINE6_BITS_PCM_IMPULSE);
  53. else
  54. line6_pcm_release(line6pcm, LINE6_BITS_PCM_IMPULSE);
  55. return count;
  56. }
  57. static DEVICE_ATTR_RW(impulse_volume);
  58. /*
  59. "read" request on "impulse_period" special file.
  60. */
  61. static ssize_t impulse_period_show(struct device *dev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. return sprintf(buf, "%d\n", dev2pcm(dev)->impulse_period);
  65. }
  66. /*
  67. "write" request on "impulse_period" special file.
  68. */
  69. static ssize_t impulse_period_store(struct device *dev,
  70. struct device_attribute *attr,
  71. const char *buf, size_t count)
  72. {
  73. int value;
  74. int ret;
  75. ret = kstrtoint(buf, 10, &value);
  76. if (ret < 0)
  77. return ret;
  78. dev2pcm(dev)->impulse_period = value;
  79. return count;
  80. }
  81. static DEVICE_ATTR_RW(impulse_period);
  82. #endif
  83. static bool test_flags(unsigned long flags0, unsigned long flags1,
  84. unsigned long mask)
  85. {
  86. return ((flags0 & mask) == 0) && ((flags1 & mask) != 0);
  87. }
  88. int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int channels)
  89. {
  90. unsigned long flags_old, flags_new, flags_final;
  91. int err;
  92. do {
  93. flags_old = ACCESS_ONCE(line6pcm->flags);
  94. flags_new = flags_old | channels;
  95. } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
  96. flags_final = flags_old;
  97. line6pcm->prev_fbuf = NULL;
  98. if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_BUFFER)) {
  99. /* Invoked multiple times in a row so allocate once only */
  100. if (!line6pcm->buffer_in) {
  101. line6pcm->buffer_in =
  102. kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
  103. line6pcm->max_packet_size, GFP_KERNEL);
  104. if (!line6pcm->buffer_in) {
  105. err = -ENOMEM;
  106. goto pcm_acquire_error;
  107. }
  108. flags_final |= channels & LINE6_BITS_CAPTURE_BUFFER;
  109. }
  110. }
  111. if (test_flags(flags_old, flags_new, LINE6_BITS_CAPTURE_STREAM)) {
  112. /*
  113. Waiting for completion of active URBs in the stop handler is
  114. a bug, we therefore report an error if capturing is restarted
  115. too soon.
  116. */
  117. if (line6pcm->active_urb_in | line6pcm->unlink_urb_in) {
  118. dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
  119. return -EBUSY;
  120. }
  121. line6pcm->count_in = 0;
  122. line6pcm->prev_fsize = 0;
  123. err = line6_submit_audio_in_all_urbs(line6pcm);
  124. if (err < 0)
  125. goto pcm_acquire_error;
  126. flags_final |= channels & LINE6_BITS_CAPTURE_STREAM;
  127. }
  128. if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_BUFFER)) {
  129. /* Invoked multiple times in a row so allocate once only */
  130. if (!line6pcm->buffer_out) {
  131. line6pcm->buffer_out =
  132. kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS *
  133. line6pcm->max_packet_size, GFP_KERNEL);
  134. if (!line6pcm->buffer_out) {
  135. err = -ENOMEM;
  136. goto pcm_acquire_error;
  137. }
  138. flags_final |= channels & LINE6_BITS_PLAYBACK_BUFFER;
  139. }
  140. }
  141. if (test_flags(flags_old, flags_new, LINE6_BITS_PLAYBACK_STREAM)) {
  142. /*
  143. See comment above regarding PCM restart.
  144. */
  145. if (line6pcm->active_urb_out | line6pcm->unlink_urb_out) {
  146. dev_err(line6pcm->line6->ifcdev, "Device not yet ready\n");
  147. return -EBUSY;
  148. }
  149. line6pcm->count_out = 0;
  150. err = line6_submit_audio_out_all_urbs(line6pcm);
  151. if (err < 0)
  152. goto pcm_acquire_error;
  153. flags_final |= channels & LINE6_BITS_PLAYBACK_STREAM;
  154. }
  155. return 0;
  156. pcm_acquire_error:
  157. /*
  158. If not all requested resources/streams could be obtained, release
  159. those which were successfully obtained (if any).
  160. */
  161. line6_pcm_release(line6pcm, flags_final & channels);
  162. return err;
  163. }
  164. int line6_pcm_release(struct snd_line6_pcm *line6pcm, int channels)
  165. {
  166. unsigned long flags_old, flags_new;
  167. do {
  168. flags_old = ACCESS_ONCE(line6pcm->flags);
  169. flags_new = flags_old & ~channels;
  170. } while (cmpxchg(&line6pcm->flags, flags_old, flags_new) != flags_old);
  171. if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_STREAM))
  172. line6_unlink_audio_in_urbs(line6pcm);
  173. if (test_flags(flags_new, flags_old, LINE6_BITS_CAPTURE_BUFFER)) {
  174. line6_wait_clear_audio_in_urbs(line6pcm);
  175. line6_free_capture_buffer(line6pcm);
  176. }
  177. if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_STREAM))
  178. line6_unlink_audio_out_urbs(line6pcm);
  179. if (test_flags(flags_new, flags_old, LINE6_BITS_PLAYBACK_BUFFER)) {
  180. line6_wait_clear_audio_out_urbs(line6pcm);
  181. line6_free_playback_buffer(line6pcm);
  182. }
  183. return 0;
  184. }
  185. /* trigger callback */
  186. int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
  187. {
  188. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  189. struct snd_pcm_substream *s;
  190. int err;
  191. unsigned long flags;
  192. spin_lock_irqsave(&line6pcm->lock_trigger, flags);
  193. clear_bit(LINE6_INDEX_PREPARED, &line6pcm->flags);
  194. snd_pcm_group_for_each_entry(s, substream) {
  195. switch (s->stream) {
  196. case SNDRV_PCM_STREAM_PLAYBACK:
  197. err = snd_line6_playback_trigger(line6pcm, cmd);
  198. if (err < 0) {
  199. spin_unlock_irqrestore(&line6pcm->lock_trigger,
  200. flags);
  201. return err;
  202. }
  203. break;
  204. case SNDRV_PCM_STREAM_CAPTURE:
  205. err = snd_line6_capture_trigger(line6pcm, cmd);
  206. if (err < 0) {
  207. spin_unlock_irqrestore(&line6pcm->lock_trigger,
  208. flags);
  209. return err;
  210. }
  211. break;
  212. default:
  213. dev_err(line6pcm->line6->ifcdev,
  214. "Unknown stream direction %d\n", s->stream);
  215. }
  216. }
  217. spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
  218. return 0;
  219. }
  220. /* control info callback */
  221. static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_info *uinfo)
  223. {
  224. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  225. uinfo->count = 2;
  226. uinfo->value.integer.min = 0;
  227. uinfo->value.integer.max = 256;
  228. return 0;
  229. }
  230. /* control get callback */
  231. static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. int i;
  235. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  236. for (i = 2; i--;)
  237. ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
  238. return 0;
  239. }
  240. /* control put callback */
  241. static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_value *ucontrol)
  243. {
  244. int i, changed = 0;
  245. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  246. for (i = 2; i--;)
  247. if (line6pcm->volume_playback[i] !=
  248. ucontrol->value.integer.value[i]) {
  249. line6pcm->volume_playback[i] =
  250. ucontrol->value.integer.value[i];
  251. changed = 1;
  252. }
  253. return changed;
  254. }
  255. /* control definition */
  256. static struct snd_kcontrol_new line6_control_playback = {
  257. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  258. .name = "PCM Playback Volume",
  259. .index = 0,
  260. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  261. .info = snd_line6_control_playback_info,
  262. .get = snd_line6_control_playback_get,
  263. .put = snd_line6_control_playback_put
  264. };
  265. /*
  266. Cleanup the PCM device.
  267. */
  268. static void line6_cleanup_pcm(struct snd_pcm *pcm)
  269. {
  270. int i;
  271. struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
  272. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  273. device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_volume);
  274. device_remove_file(line6pcm->line6->ifcdev, &dev_attr_impulse_period);
  275. #endif
  276. for (i = LINE6_ISO_BUFFERS; i--;) {
  277. if (line6pcm->urb_audio_out[i]) {
  278. usb_kill_urb(line6pcm->urb_audio_out[i]);
  279. usb_free_urb(line6pcm->urb_audio_out[i]);
  280. }
  281. if (line6pcm->urb_audio_in[i]) {
  282. usb_kill_urb(line6pcm->urb_audio_in[i]);
  283. usb_free_urb(line6pcm->urb_audio_in[i]);
  284. }
  285. }
  286. }
  287. /* create a PCM device */
  288. static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
  289. {
  290. struct snd_pcm *pcm;
  291. int err;
  292. err = snd_pcm_new(line6pcm->line6->card,
  293. (char *)line6pcm->line6->properties->name,
  294. 0, 1, 1, &pcm);
  295. if (err < 0)
  296. return err;
  297. pcm->private_data = line6pcm;
  298. pcm->private_free = line6_cleanup_pcm;
  299. line6pcm->pcm = pcm;
  300. strcpy(pcm->name, line6pcm->line6->properties->name);
  301. /* set operators */
  302. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  303. &snd_line6_playback_ops);
  304. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
  305. /* pre-allocation of buffers */
  306. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  307. snd_dma_continuous_data
  308. (GFP_KERNEL), 64 * 1024,
  309. 128 * 1024);
  310. return 0;
  311. }
  312. /* PCM device destructor */
  313. static int snd_line6_pcm_free(struct snd_device *device)
  314. {
  315. return 0;
  316. }
  317. /*
  318. Stop substream if still running.
  319. */
  320. static void pcm_disconnect_substream(struct snd_pcm_substream *substream)
  321. {
  322. if (substream->runtime && snd_pcm_running(substream)) {
  323. snd_pcm_stream_lock_irq(substream);
  324. snd_pcm_stop(substream, SNDRV_PCM_STATE_DISCONNECTED);
  325. snd_pcm_stream_unlock_irq(substream);
  326. }
  327. }
  328. /*
  329. Stop PCM stream.
  330. */
  331. void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
  332. {
  333. pcm_disconnect_substream(get_substream
  334. (line6pcm, SNDRV_PCM_STREAM_CAPTURE));
  335. pcm_disconnect_substream(get_substream
  336. (line6pcm, SNDRV_PCM_STREAM_PLAYBACK));
  337. line6_unlink_wait_clear_audio_out_urbs(line6pcm);
  338. line6_unlink_wait_clear_audio_in_urbs(line6pcm);
  339. }
  340. /*
  341. Create and register the PCM device and mixer entries.
  342. Create URBs for playback and capture.
  343. */
  344. int line6_init_pcm(struct usb_line6 *line6,
  345. struct line6_pcm_properties *properties)
  346. {
  347. static struct snd_device_ops pcm_ops = {
  348. .dev_free = snd_line6_pcm_free,
  349. };
  350. int err;
  351. int ep_read = 0, ep_write = 0;
  352. struct snd_line6_pcm *line6pcm;
  353. if (!(line6->properties->capabilities & LINE6_BIT_PCM))
  354. return 0; /* skip PCM initialization and report success */
  355. /* initialize PCM subsystem based on product id: */
  356. switch (line6->product) {
  357. case LINE6_DEVID_BASSPODXT:
  358. case LINE6_DEVID_BASSPODXTLIVE:
  359. case LINE6_DEVID_BASSPODXTPRO:
  360. case LINE6_DEVID_PODXT:
  361. case LINE6_DEVID_PODXTLIVE:
  362. case LINE6_DEVID_PODXTPRO:
  363. case LINE6_DEVID_PODHD300:
  364. case LINE6_DEVID_PODHD400:
  365. ep_read = 0x82;
  366. ep_write = 0x01;
  367. break;
  368. case LINE6_DEVID_PODHD500:
  369. case LINE6_DEVID_PODX3:
  370. case LINE6_DEVID_PODX3LIVE:
  371. ep_read = 0x86;
  372. ep_write = 0x02;
  373. break;
  374. case LINE6_DEVID_POCKETPOD:
  375. ep_read = 0x82;
  376. ep_write = 0x02;
  377. break;
  378. case LINE6_DEVID_GUITARPORT:
  379. case LINE6_DEVID_PODSTUDIO_GX:
  380. case LINE6_DEVID_PODSTUDIO_UX1:
  381. case LINE6_DEVID_PODSTUDIO_UX2:
  382. case LINE6_DEVID_TONEPORT_GX:
  383. case LINE6_DEVID_TONEPORT_UX1:
  384. case LINE6_DEVID_TONEPORT_UX2:
  385. ep_read = 0x82;
  386. ep_write = 0x01;
  387. break;
  388. /* this is for interface_number == 1:
  389. case LINE6_DEVID_TONEPORT_UX2:
  390. case LINE6_DEVID_PODSTUDIO_UX2:
  391. ep_read = 0x87;
  392. ep_write = 0x00;
  393. break; */
  394. default:
  395. MISSING_CASE;
  396. }
  397. line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
  398. if (line6pcm == NULL)
  399. return -ENOMEM;
  400. line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
  401. line6pcm->volume_monitor = 255;
  402. line6pcm->line6 = line6;
  403. line6pcm->ep_audio_read = ep_read;
  404. line6pcm->ep_audio_write = ep_write;
  405. /* Read and write buffers are sized identically, so choose minimum */
  406. line6pcm->max_packet_size = min(
  407. usb_maxpacket(line6->usbdev,
  408. usb_rcvisocpipe(line6->usbdev, ep_read), 0),
  409. usb_maxpacket(line6->usbdev,
  410. usb_sndisocpipe(line6->usbdev, ep_write), 1));
  411. line6pcm->properties = properties;
  412. line6->line6pcm = line6pcm;
  413. /* PCM device: */
  414. err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops);
  415. if (err < 0)
  416. return err;
  417. err = snd_line6_new_pcm(line6pcm);
  418. if (err < 0)
  419. return err;
  420. spin_lock_init(&line6pcm->lock_audio_out);
  421. spin_lock_init(&line6pcm->lock_audio_in);
  422. spin_lock_init(&line6pcm->lock_trigger);
  423. err = line6_create_audio_out_urbs(line6pcm);
  424. if (err < 0)
  425. return err;
  426. err = line6_create_audio_in_urbs(line6pcm);
  427. if (err < 0)
  428. return err;
  429. /* mixer: */
  430. err =
  431. snd_ctl_add(line6->card,
  432. snd_ctl_new1(&line6_control_playback, line6pcm));
  433. if (err < 0)
  434. return err;
  435. #ifdef CONFIG_LINE6_USB_IMPULSE_RESPONSE
  436. /* impulse response test: */
  437. err = device_create_file(line6->ifcdev, &dev_attr_impulse_volume);
  438. if (err < 0)
  439. return err;
  440. err = device_create_file(line6->ifcdev, &dev_attr_impulse_period);
  441. if (err < 0)
  442. return err;
  443. line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
  444. #endif
  445. return 0;
  446. }
  447. /* prepare pcm callback */
  448. int snd_line6_prepare(struct snd_pcm_substream *substream)
  449. {
  450. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  451. switch (substream->stream) {
  452. case SNDRV_PCM_STREAM_PLAYBACK:
  453. if ((line6pcm->flags & LINE6_BITS_PLAYBACK_STREAM) == 0)
  454. line6_unlink_wait_clear_audio_out_urbs(line6pcm);
  455. break;
  456. case SNDRV_PCM_STREAM_CAPTURE:
  457. if ((line6pcm->flags & LINE6_BITS_CAPTURE_STREAM) == 0)
  458. line6_unlink_wait_clear_audio_in_urbs(line6pcm);
  459. break;
  460. default:
  461. MISSING_CASE;
  462. }
  463. if (!test_and_set_bit(LINE6_INDEX_PREPARED, &line6pcm->flags)) {
  464. line6pcm->count_out = 0;
  465. line6pcm->pos_out = 0;
  466. line6pcm->pos_out_done = 0;
  467. line6pcm->bytes_out = 0;
  468. line6pcm->count_in = 0;
  469. line6pcm->pos_in_done = 0;
  470. line6pcm->bytes_in = 0;
  471. }
  472. return 0;
  473. }