pod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Line 6 Linux USB driver
  4. *
  5. * Copyright (C) 2004-2010 Markus Grabner ([email protected])
  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. 0xf0, 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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 PCM subsystem: */
  324. err = line6_init_pcm(line6, &pod_pcm_properties);
  325. if (err < 0)
  326. return err;
  327. /* register monitor control: */
  328. err = snd_ctl_add(line6->card,
  329. snd_ctl_new1(&pod_control_monitor, line6->line6pcm));
  330. if (err < 0)
  331. return err;
  332. /*
  333. When the sound card is registered at this point, the PODxt Live
  334. displays "Invalid Code Error 07", so we do it later in the event
  335. handler.
  336. */
  337. if (pod->line6.properties->capabilities & LINE6_CAP_CONTROL) {
  338. pod->monitor_level = POD_SYSTEM_INVALID;
  339. /* initiate startup procedure: */
  340. schedule_delayed_work(&line6->startup_work,
  341. msecs_to_jiffies(POD_STARTUP_DELAY));
  342. }
  343. return 0;
  344. }
  345. #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
  346. #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
  347. /* table of devices that work with this driver */
  348. static const struct usb_device_id pod_id_table[] = {
  349. { LINE6_DEVICE(0x4250), .driver_info = LINE6_BASSPODXT },
  350. { LINE6_DEVICE(0x4642), .driver_info = LINE6_BASSPODXTLIVE },
  351. { LINE6_DEVICE(0x4252), .driver_info = LINE6_BASSPODXTPRO },
  352. { LINE6_IF_NUM(0x5051, 1), .driver_info = LINE6_POCKETPOD },
  353. { LINE6_DEVICE(0x5044), .driver_info = LINE6_PODXT },
  354. { LINE6_IF_NUM(0x4650, 0), .driver_info = LINE6_PODXTLIVE_POD },
  355. { LINE6_DEVICE(0x5050), .driver_info = LINE6_PODXTPRO },
  356. {}
  357. };
  358. MODULE_DEVICE_TABLE(usb, pod_id_table);
  359. static const struct line6_properties pod_properties_table[] = {
  360. [LINE6_BASSPODXT] = {
  361. .id = "BassPODxt",
  362. .name = "BassPODxt",
  363. .capabilities = LINE6_CAP_CONTROL
  364. | LINE6_CAP_CONTROL_MIDI
  365. | LINE6_CAP_PCM
  366. | LINE6_CAP_HWMON,
  367. .altsetting = 5,
  368. .ep_ctrl_r = 0x84,
  369. .ep_ctrl_w = 0x03,
  370. .ep_audio_r = 0x82,
  371. .ep_audio_w = 0x01,
  372. },
  373. [LINE6_BASSPODXTLIVE] = {
  374. .id = "BassPODxtLive",
  375. .name = "BassPODxt Live",
  376. .capabilities = LINE6_CAP_CONTROL
  377. | LINE6_CAP_CONTROL_MIDI
  378. | LINE6_CAP_PCM
  379. | LINE6_CAP_HWMON,
  380. .altsetting = 1,
  381. .ep_ctrl_r = 0x84,
  382. .ep_ctrl_w = 0x03,
  383. .ep_audio_r = 0x82,
  384. .ep_audio_w = 0x01,
  385. },
  386. [LINE6_BASSPODXTPRO] = {
  387. .id = "BassPODxtPro",
  388. .name = "BassPODxt Pro",
  389. .capabilities = LINE6_CAP_CONTROL
  390. | LINE6_CAP_CONTROL_MIDI
  391. | LINE6_CAP_PCM
  392. | LINE6_CAP_HWMON,
  393. .altsetting = 5,
  394. .ep_ctrl_r = 0x84,
  395. .ep_ctrl_w = 0x03,
  396. .ep_audio_r = 0x82,
  397. .ep_audio_w = 0x01,
  398. },
  399. [LINE6_POCKETPOD] = {
  400. .id = "PocketPOD",
  401. .name = "Pocket POD",
  402. .capabilities = LINE6_CAP_CONTROL
  403. | LINE6_CAP_CONTROL_MIDI,
  404. .altsetting = 0,
  405. .ep_ctrl_r = 0x82,
  406. .ep_ctrl_w = 0x02,
  407. /* no audio channel */
  408. },
  409. [LINE6_PODXT] = {
  410. .id = "PODxt",
  411. .name = "PODxt",
  412. .capabilities = LINE6_CAP_CONTROL
  413. | LINE6_CAP_CONTROL_MIDI
  414. | LINE6_CAP_PCM
  415. | LINE6_CAP_HWMON,
  416. .altsetting = 5,
  417. .ep_ctrl_r = 0x84,
  418. .ep_ctrl_w = 0x03,
  419. .ep_audio_r = 0x82,
  420. .ep_audio_w = 0x01,
  421. },
  422. [LINE6_PODXTLIVE_POD] = {
  423. .id = "PODxtLive",
  424. .name = "PODxt Live",
  425. .capabilities = LINE6_CAP_CONTROL
  426. | LINE6_CAP_CONTROL_MIDI
  427. | LINE6_CAP_PCM
  428. | LINE6_CAP_HWMON,
  429. .altsetting = 1,
  430. .ep_ctrl_r = 0x84,
  431. .ep_ctrl_w = 0x03,
  432. .ep_audio_r = 0x82,
  433. .ep_audio_w = 0x01,
  434. },
  435. [LINE6_PODXTPRO] = {
  436. .id = "PODxtPro",
  437. .name = "PODxt Pro",
  438. .capabilities = LINE6_CAP_CONTROL
  439. | LINE6_CAP_CONTROL_MIDI
  440. | LINE6_CAP_PCM
  441. | LINE6_CAP_HWMON,
  442. .altsetting = 5,
  443. .ep_ctrl_r = 0x84,
  444. .ep_ctrl_w = 0x03,
  445. .ep_audio_r = 0x82,
  446. .ep_audio_w = 0x01,
  447. },
  448. };
  449. /*
  450. Probe USB device.
  451. */
  452. static int pod_probe(struct usb_interface *interface,
  453. const struct usb_device_id *id)
  454. {
  455. return line6_probe(interface, id, "Line6-POD",
  456. &pod_properties_table[id->driver_info],
  457. pod_init, sizeof(struct usb_line6_pod));
  458. }
  459. static struct usb_driver pod_driver = {
  460. .name = KBUILD_MODNAME,
  461. .probe = pod_probe,
  462. .disconnect = line6_disconnect,
  463. #ifdef CONFIG_PM
  464. .suspend = line6_suspend,
  465. .resume = line6_resume,
  466. .reset_resume = line6_resume,
  467. #endif
  468. .id_table = pod_id_table,
  469. };
  470. module_usb_driver(pod_driver);
  471. MODULE_DESCRIPTION("Line 6 POD USB driver");
  472. MODULE_LICENSE("GPL");