PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/sound/usb/line6/pod.c

http://github.com/torvalds/linux
C | 546 lines | 402 code | 70 blank | 74 comment | 23 complexity | 727e8ef30c02f3189ab88b4b82902c16 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Line 6 Linux USB driver
  4. *
  5. * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/wait.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <linux/usb.h>
  12. #include <sound/core.h>
  13. #include <sound/control.h>
  14. #include "capture.h"
  15. #include "driver.h"
  16. #include "playback.h"
  17. /*
  18. Locate name in binary program dump
  19. */
  20. #define POD_NAME_OFFSET 0
  21. #define POD_NAME_LENGTH 16
  22. /*
  23. Other constants
  24. */
  25. #define POD_CONTROL_SIZE 0x80
  26. #define POD_BUFSIZE_DUMPREQ 7
  27. #define POD_STARTUP_DELAY 1000
  28. /*
  29. Stages of POD startup procedure
  30. */
  31. enum {
  32. POD_STARTUP_VERSIONREQ,
  33. POD_STARTUP_SETUP,
  34. POD_STARTUP_DONE,
  35. };
  36. enum {
  37. LINE6_BASSPODXT,
  38. LINE6_BASSPODXTLIVE,
  39. LINE6_BASSPODXTPRO,
  40. LINE6_POCKETPOD,
  41. LINE6_PODXT,
  42. LINE6_PODXTLIVE_POD,
  43. LINE6_PODXTPRO,
  44. };
  45. struct usb_line6_pod {
  46. /* Generic Line 6 USB data */
  47. struct usb_line6 line6;
  48. /* Instrument monitor level */
  49. int monitor_level;
  50. /* Current progress in startup procedure */
  51. int startup_progress;
  52. /* Serial number of device */
  53. u32 serial_number;
  54. /* Firmware version (x 100) */
  55. int firmware_version;
  56. /* Device ID */
  57. int device_id;
  58. };
  59. #define line6_to_pod(x) container_of(x, struct usb_line6_pod, line6)
  60. #define POD_SYSEX_CODE 3
  61. /* *INDENT-OFF* */
  62. enum {
  63. POD_SYSEX_SAVE = 0x24,
  64. POD_SYSEX_SYSTEM = 0x56,
  65. POD_SYSEX_SYSTEMREQ = 0x57,
  66. /* POD_SYSEX_UPDATE = 0x6c, */ /* software update! */
  67. POD_SYSEX_STORE = 0x71,
  68. POD_SYSEX_FINISH = 0x72,
  69. POD_SYSEX_DUMPMEM = 0x73,
  70. POD_SYSEX_DUMP = 0x74,
  71. POD_SYSEX_DUMPREQ = 0x75
  72. /* dumps entire internal memory of PODxt Pro */
  73. /* POD_SYSEX_DUMPMEM2 = 0x76 */
  74. };
  75. enum {
  76. POD_MONITOR_LEVEL = 0x04,
  77. POD_SYSTEM_INVALID = 0x10000
  78. };
  79. /* *INDENT-ON* */
  80. enum {
  81. POD_DUMP_MEMORY = 2
  82. };
  83. enum {
  84. POD_BUSY_READ,
  85. POD_BUSY_WRITE,
  86. POD_CHANNEL_DIRTY,
  87. POD_SAVE_PRESSED,
  88. POD_BUSY_MIDISEND
  89. };
  90. static const struct snd_ratden pod_ratden = {
  91. .num_min = 78125,
  92. .num_max = 78125,
  93. .num_step = 1,
  94. .den = 2
  95. };
  96. static struct line6_pcm_properties pod_pcm_properties = {
  97. .playback_hw = {
  98. .info = (SNDRV_PCM_INFO_MMAP |
  99. SNDRV_PCM_INFO_INTERLEAVED |
  100. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  101. SNDRV_PCM_INFO_MMAP_VALID |
  102. SNDRV_PCM_INFO_PAUSE |
  103. SNDRV_PCM_INFO_SYNC_START),
  104. .formats = SNDRV_PCM_FMTBIT_S24_3LE,
  105. .rates = SNDRV_PCM_RATE_KNOT,
  106. .rate_min = 39062,
  107. .rate_max = 39063,
  108. .channels_min = 2,
  109. .channels_max = 2,
  110. .buffer_bytes_max = 60000,
  111. .period_bytes_min = 64,
  112. .period_bytes_max = 8192,
  113. .periods_min = 1,
  114. .periods_max = 1024},
  115. .capture_hw = {
  116. .info = (SNDRV_PCM_INFO_MMAP |
  117. SNDRV_PCM_INFO_INTERLEAVED |
  118. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  119. SNDRV_PCM_INFO_MMAP_VALID |
  120. SNDRV_PCM_INFO_SYNC_START),
  121. .formats = SNDRV_PCM_FMTBIT_S24_3LE,
  122. .rates = SNDRV_PCM_RATE_KNOT,
  123. .rate_min = 39062,
  124. .rate_max = 39063,
  125. .channels_min = 2,
  126. .channels_max = 2,
  127. .buffer_bytes_max = 60000,
  128. .period_bytes_min = 64,
  129. .period_bytes_max = 8192,
  130. .periods_min = 1,
  131. .periods_max = 1024},
  132. .rates = {
  133. .nrats = 1,
  134. .rats = &pod_ratden},
  135. .bytes_per_channel = 3 /* SNDRV_PCM_FMTBIT_S24_3LE */
  136. };
  137. static const char pod_version_header[] = {
  138. 0xf2, 0x7e, 0x7f, 0x06, 0x02
  139. };
  140. static char *pod_alloc_sysex_buffer(struct usb_line6_pod *pod, int code,
  141. int size)
  142. {
  143. return line6_alloc_sysex_buffer(&pod->line6, POD_SYSEX_CODE, code,
  144. size);
  145. }
  146. /*
  147. Process a completely received message.
  148. */
  149. static void line6_pod_process_message(struct usb_line6 *line6)
  150. {
  151. struct usb_line6_pod *pod = line6_to_pod(line6);
  152. const unsigned char *buf = pod->line6.buffer_message;
  153. if (memcmp(buf, pod_version_header, sizeof(pod_version_header)) == 0) {
  154. pod->firmware_version = buf[13] * 100 + buf[14] * 10 + buf[15];
  155. pod->device_id = ((int)buf[8] << 16) | ((int)buf[9] << 8) |
  156. (int) buf[10];
  157. if (pod->startup_progress == POD_STARTUP_VERSIONREQ) {
  158. pod->startup_progress = POD_STARTUP_SETUP;
  159. schedule_delayed_work(&line6->startup_work, 0);
  160. }
  161. return;
  162. }
  163. /* Only look for sysex messages from this device */
  164. if (buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_DEVICE) &&
  165. buf[0] != (LINE6_SYSEX_BEGIN | LINE6_CHANNEL_UNKNOWN)) {
  166. return;
  167. }
  168. if (memcmp(buf + 1, line6_midi_id, sizeof(line6_midi_id)) != 0)
  169. return;
  170. if (buf[5] == POD_SYSEX_SYSTEM && buf[6] == POD_MONITOR_LEVEL) {
  171. short value = ((int)buf[7] << 12) | ((int)buf[8] << 8) |
  172. ((int)buf[9] << 4) | (int)buf[10];
  173. pod->monitor_level = value;
  174. }
  175. }
  176. /*
  177. Send system parameter (from integer).
  178. */
  179. static int pod_set_system_param_int(struct usb_line6_pod *pod, int value,
  180. int code)
  181. {
  182. char *sysex;
  183. static const int size = 5;
  184. sysex = pod_alloc_sysex_buffer(pod, POD_SYSEX_SYSTEM, size);
  185. if (!sysex)
  186. return -ENOMEM;
  187. sysex[SYSEX_DATA_OFS] = code;
  188. sysex[SYSEX_DATA_OFS + 1] = (value >> 12) & 0x0f;
  189. sysex[SYSEX_DATA_OFS + 2] = (value >> 8) & 0x0f;
  190. sysex[SYSEX_DATA_OFS + 3] = (value >> 4) & 0x0f;
  191. sysex[SYSEX_DATA_OFS + 4] = (value) & 0x0f;
  192. line6_send_sysex_message(&pod->line6, sysex, size);
  193. kfree(sysex);
  194. return 0;
  195. }
  196. /*
  197. "read" request on "serial_number" special file.
  198. */
  199. static ssize_t serial_number_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct snd_card *card = dev_to_snd_card(dev);
  203. struct usb_line6_pod *pod = card->private_data;
  204. return sprintf(buf, "%u\n", pod->serial_number);
  205. }
  206. /*
  207. "read" request on "firmware_version" special file.
  208. */
  209. static ssize_t firmware_version_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct snd_card *card = dev_to_snd_card(dev);
  213. struct usb_line6_pod *pod = card->private_data;
  214. return sprintf(buf, "%d.%02d\n", pod->firmware_version / 100,
  215. pod->firmware_version % 100);
  216. }
  217. /*
  218. "read" request on "device_id" special file.
  219. */
  220. static ssize_t device_id_show(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. struct snd_card *card = dev_to_snd_card(dev);
  224. struct usb_line6_pod *pod = card->private_data;
  225. return sprintf(buf, "%d\n", pod->device_id);
  226. }
  227. /*
  228. POD startup procedure.
  229. This is a sequence of functions with special requirements (e.g., must
  230. not run immediately after initialization, must not run in interrupt
  231. context). After the last one has finished, the device is ready to use.
  232. */
  233. static void pod_startup(struct usb_line6 *line6)
  234. {
  235. struct usb_line6_pod *pod = line6_to_pod(line6);
  236. switch (pod->startup_progress) {
  237. case POD_STARTUP_VERSIONREQ:
  238. /* request firmware version: */
  239. line6_version_request_async(line6);
  240. break;
  241. case POD_STARTUP_SETUP:
  242. /* serial number: */
  243. line6_read_serial_number(&pod->line6, &pod->serial_number);
  244. /* ALSA audio interface: */
  245. if (snd_card_register(line6->card))
  246. dev_err(line6->ifcdev, "Failed to register POD card.\n");
  247. pod->startup_progress = POD_STARTUP_DONE;
  248. break;
  249. default:
  250. break;
  251. }
  252. }
  253. /* POD special files: */
  254. static DEVICE_ATTR_RO(device_id);
  255. static DEVICE_ATTR_RO(firmware_version);
  256. static DEVICE_ATTR_RO(serial_number);
  257. static struct attribute *pod_dev_attrs[] = {
  258. &dev_attr_device_id.attr,
  259. &dev_attr_firmware_version.attr,
  260. &dev_attr_serial_number.attr,
  261. NULL
  262. };
  263. static const struct attribute_group pod_dev_attr_group = {
  264. .name = "pod",
  265. .attrs = pod_dev_attrs,
  266. };
  267. /* control info callback */
  268. static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol,
  269. struct snd_ctl_elem_info *uinfo)
  270. {
  271. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  272. uinfo->count = 1;
  273. uinfo->value.integer.min = 0;
  274. uinfo->value.integer.max = 65535;
  275. return 0;
  276. }
  277. /* control get callback */
  278. static int snd_pod_control_monitor_get(struct snd_kcontrol *kcontrol,
  279. struct snd_ctl_elem_value *ucontrol)
  280. {
  281. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  282. struct usb_line6_pod *pod = line6_to_pod(line6pcm->line6);
  283. ucontrol->value.integer.value[0] = pod->monitor_level;
  284. return 0;
  285. }
  286. /* control put callback */
  287. static int snd_pod_control_monitor_put(struct snd_kcontrol *kcontrol,
  288. struct snd_ctl_elem_value *ucontrol)
  289. {
  290. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  291. struct usb_line6_pod *pod = line6_to_pod(line6pcm->line6);
  292. if (ucontrol->value.integer.value[0] == pod->monitor_level)
  293. return 0;
  294. pod->monitor_level = ucontrol->value.integer.value[0];
  295. pod_set_system_param_int(pod, ucontrol->value.integer.value[0],
  296. POD_MONITOR_LEVEL);
  297. return 1;
  298. }
  299. /* control definition */
  300. static const struct snd_kcontrol_new pod_control_monitor = {
  301. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  302. .name = "Monitor Playback Volume",
  303. .index = 0,
  304. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  305. .info = snd_pod_control_monitor_info,
  306. .get = snd_pod_control_monitor_get,
  307. .put = snd_pod_control_monitor_put
  308. };
  309. /*
  310. Try to init POD device.
  311. */
  312. static int pod_init(struct usb_line6 *line6,
  313. const struct usb_device_id *id)
  314. {
  315. int err;
  316. struct usb_line6_pod *pod = line6_to_pod(line6);
  317. line6->process_message = line6_pod_process_message;
  318. line6->startup = pod_startup;
  319. /* create sysfs entries: */
  320. err = snd_card_add_dev_attr(line6->card, &pod_dev_attr_group);
  321. if (err < 0)
  322. return err;
  323. /* initialize MIDI subsystem: */
  324. err = line6_init_midi(line6);
  325. if (err < 0)
  326. return err;
  327. /* initialize PCM subsystem: */
  328. err = line6_init_pcm(line6, &pod_pcm_properties);
  329. if (err < 0)
  330. return err;
  331. /* register monitor control: */
  332. err = snd_ctl_add(line6->card,
  333. snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
  334. if (err < 0)
  335. return err;
  336. /*
  337. When the sound card is registered at this point, the PODxt Live
  338. displays "Invalid Code Error 07", so we do it later in the event
  339. handler.
  340. */
  341. if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
  342. pod->monitor_level = POD_SYSTEM_INVALID;
  343. /* initiate startup procedure: */
  344. schedule_delayed_work(&line6->startup_work,
  345. msecs_to_jiffies(POD_STARTUP_DELAY));
  346. }
  347. return 0;
  348. }
  349. #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
  350. #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
  351. /* table of devices that work with this driver */
  352. static const struct usb_device_id pod_id_table[] = {
  353. { LINE6_DEVICE(0x4250), .driver_info = LINE6_BASSPODXT },
  354. { LINE6_DEVICE(0x4642), .driver_info = LINE6_BASSPODXTLIVE },
  355. { LINE6_DEVICE(0x4252), .driver_info = LINE6_BASSPODXTPRO },
  356. { LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
  357. { LINE6_DEVICE(0x5044), .driver_info = LINE6_PODXT },
  358. { LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
  359. { LINE6_DEVICE(0x5050), .driver_info = LINE6_PODXTPRO },
  360. {}
  361. };
  362. MODULE_DEVICE_TABLE(usb, pod_id_table);
  363. static const struct line6_properties pod_properties_table[] = {
  364. [LINE6_BASSPODXT] = {
  365. .id = "BassPODxt",
  366. .name = "BassPODxt",
  367. .capabilities = LINE6_CAP_CONTROL
  368. | LINE6_CAP_CONTROL_MIDI
  369. | LINE6_CAP_PCM
  370. | LINE6_CAP_HWMON,
  371. .altsetting = 5,
  372. .ep_ctrl_r = 0x84,
  373. .ep_ctrl_w = 0x03,
  374. .ep_audio_r = 0x82,
  375. .ep_audio_w = 0x01,
  376. },
  377. [LINE6_BASSPODXTLIVE] = {
  378. .id = "BassPODxtLive",
  379. .name = "BassPODxt Live",
  380. .capabilities = LINE6_CAP_CONTROL
  381. | LINE6_CAP_CONTROL_MIDI
  382. | LINE6_CAP_PCM
  383. | LINE6_CAP_HWMON,
  384. .altsetting = 1,
  385. .ep_ctrl_r = 0x84,
  386. .ep_ctrl_w = 0x03,
  387. .ep_audio_r = 0x82,
  388. .ep_audio_w = 0x01,
  389. },
  390. [LINE6_BASSPODXTPRO] = {
  391. .id = "BassPODxtPro",
  392. .name = "BassPODxt Pro",
  393. .capabilities = LINE6_CAP_CONTROL
  394. | LINE6_CAP_CONTROL_MIDI
  395. | LINE6_CAP_PCM
  396. | LINE6_CAP_HWMON,
  397. .altsetting = 5,
  398. .ep_ctrl_r = 0x84,
  399. .ep_ctrl_w = 0x03,
  400. .ep_audio_r = 0x82,
  401. .ep_audio_w = 0x01,
  402. },
  403. [LINE6_POCKETPOD] = {
  404. .id = "PocketPOD",
  405. .name = "Pocket POD",
  406. .capabilities = LINE6_CAP_CONTROL
  407. | LINE6_CAP_CONTROL_MIDI,
  408. .altsetting = 0,
  409. .ep_ctrl_r = 0x82,
  410. .ep_ctrl_w = 0x02,
  411. /* no audio channel */
  412. },
  413. [LINE6_PODXT] = {
  414. .id = "PODxt",
  415. .name = "PODxt",
  416. .capabilities = LINE6_CAP_CONTROL
  417. | LINE6_CAP_CONTROL_MIDI
  418. | LINE6_CAP_PCM
  419. | LINE6_CAP_HWMON,
  420. .altsetting = 5,
  421. .ep_ctrl_r = 0x84,
  422. .ep_ctrl_w = 0x03,
  423. .ep_audio_r = 0x82,
  424. .ep_audio_w = 0x01,
  425. },
  426. [LINE6_PODXTLIVE_POD] = {
  427. .id = "PODxtLive",
  428. .name = "PODxt Live",
  429. .capabilities = LINE6_CAP_CONTROL
  430. | LINE6_CAP_CONTROL_MIDI
  431. | LINE6_CAP_PCM
  432. | LINE6_CAP_HWMON,
  433. .altsetting = 1,
  434. .ep_ctrl_r = 0x84,
  435. .ep_ctrl_w = 0x03,
  436. .ep_audio_r = 0x82,
  437. .ep_audio_w = 0x01,
  438. },
  439. [LINE6_PODXTPRO] = {
  440. .id = "PODxtPro",
  441. .name = "PODxt Pro",
  442. .capabilities = LINE6_CAP_CONTROL
  443. | LINE6_CAP_CONTROL_MIDI
  444. | LINE6_CAP_PCM
  445. | LINE6_CAP_HWMON,
  446. .altsetting = 5,
  447. .ep_ctrl_r = 0x84,
  448. .ep_ctrl_w = 0x03,
  449. .ep_audio_r = 0x82,
  450. .ep_audio_w = 0x01,
  451. },
  452. };
  453. /*
  454. Probe USB device.
  455. */
  456. static int pod_probe(struct usb_interface *interface,
  457. const struct usb_device_id *id)
  458. {
  459. return line6_probe(interface, id, "Line6-POD",
  460. &pod_properties_table[id->driver_info],
  461. pod_init, sizeof(struct usb_line6_pod));
  462. }
  463. static struct usb_driver pod_driver = {
  464. .name = KBUILD_MODNAME,
  465. .probe = pod_probe,
  466. .disconnect = line6_disconnect,
  467. #ifdef CONFIG_PM
  468. .suspend = line6_suspend,
  469. .resume = line6_resume,
  470. .reset_resume = line6_resume,
  471. #endif
  472. .id_table = pod_id_table,
  473. };
  474. module_usb_driver(pod_driver);
  475. MODULE_DESCRIPTION("Line 6 POD USB driver");
  476. MODULE_LICENSE("GPL");