toneport.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. * Emil Myhrman ([email protected])
  7. */
  8. #include <linux/wait.h>
  9. #include <linux/usb.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/leds.h>
  13. #include <sound/core.h>
  14. #include <sound/control.h>
  15. #include "capture.h"
  16. #include "driver.h"
  17. #include "playback.h"
  18. enum line6_device_type {
  19. LINE6_GUITARPORT,
  20. LINE6_PODSTUDIO_GX,
  21. LINE6_PODSTUDIO_UX1,
  22. LINE6_PODSTUDIO_UX2,
  23. LINE6_TONEPORT_GX,
  24. LINE6_TONEPORT_UX1,
  25. LINE6_TONEPORT_UX2,
  26. };
  27. struct usb_line6_toneport;
  28. struct toneport_led {
  29. struct led_classdev dev;
  30. char name[64];
  31. struct usb_line6_toneport *toneport;
  32. bool registered;
  33. };
  34. struct usb_line6_toneport {
  35. /* Generic Line 6 USB data */
  36. struct usb_line6 line6;
  37. /* Source selector */
  38. int source;
  39. /* Serial number of device */
  40. u32 serial_number;
  41. /* Firmware version (x 100) */
  42. u8 firmware_version;
  43. /* Device type */
  44. enum line6_device_type type;
  45. /* LED instances */
  46. struct toneport_led leds[2];
  47. };
  48. #define line6_to_toneport(x) container_of(x, struct usb_line6_toneport, line6)
  49. static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2);
  50. #define TONEPORT_PCM_DELAY 1
  51. static const struct snd_ratden toneport_ratden = {
  52. .num_min = 44100,
  53. .num_max = 44100,
  54. .num_step = 1,
  55. .den = 1
  56. };
  57. static struct line6_pcm_properties toneport_pcm_properties = {
  58. .playback_hw = {
  59. .info = (SNDRV_PCM_INFO_MMAP |
  60. SNDRV_PCM_INFO_INTERLEAVED |
  61. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  62. SNDRV_PCM_INFO_MMAP_VALID |
  63. SNDRV_PCM_INFO_PAUSE |
  64. SNDRV_PCM_INFO_SYNC_START),
  65. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  66. .rates = SNDRV_PCM_RATE_KNOT,
  67. .rate_min = 44100,
  68. .rate_max = 44100,
  69. .channels_min = 2,
  70. .channels_max = 2,
  71. .buffer_bytes_max = 60000,
  72. .period_bytes_min = 64,
  73. .period_bytes_max = 8192,
  74. .periods_min = 1,
  75. .periods_max = 1024},
  76. .capture_hw = {
  77. .info = (SNDRV_PCM_INFO_MMAP |
  78. SNDRV_PCM_INFO_INTERLEAVED |
  79. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  80. SNDRV_PCM_INFO_MMAP_VALID |
  81. SNDRV_PCM_INFO_SYNC_START),
  82. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  83. .rates = SNDRV_PCM_RATE_KNOT,
  84. .rate_min = 44100,
  85. .rate_max = 44100,
  86. .channels_min = 2,
  87. .channels_max = 2,
  88. .buffer_bytes_max = 60000,
  89. .period_bytes_min = 64,
  90. .period_bytes_max = 8192,
  91. .periods_min = 1,
  92. .periods_max = 1024},
  93. .rates = {
  94. .nrats = 1,
  95. .rats = &toneport_ratden},
  96. .bytes_per_channel = 2
  97. };
  98. static const struct {
  99. const char *name;
  100. int code;
  101. } toneport_source_info[] = {
  102. {"Microphone", 0x0a01},
  103. {"Line", 0x0801},
  104. {"Instrument", 0x0b01},
  105. {"Inst & Mic", 0x0901}
  106. };
  107. static int toneport_send_cmd(struct usb_device *usbdev, int cmd1, int cmd2)
  108. {
  109. int ret;
  110. ret = usb_control_msg_send(usbdev, 0, 0x67,
  111. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  112. cmd1, cmd2, NULL, 0, LINE6_TIMEOUT,
  113. GFP_KERNEL);
  114. if (ret) {
  115. dev_err(&usbdev->dev, "send failed (error %d)\n", ret);
  116. return ret;
  117. }
  118. return 0;
  119. }
  120. /* monitor info callback */
  121. static int snd_toneport_monitor_info(struct snd_kcontrol *kcontrol,
  122. struct snd_ctl_elem_info *uinfo)
  123. {
  124. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  125. uinfo->count = 1;
  126. uinfo->value.integer.min = 0;
  127. uinfo->value.integer.max = 256;
  128. return 0;
  129. }
  130. /* monitor get callback */
  131. static int snd_toneport_monitor_get(struct snd_kcontrol *kcontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  135. ucontrol->value.integer.value[0] = line6pcm->volume_monitor;
  136. return 0;
  137. }
  138. /* monitor put callback */
  139. static int snd_toneport_monitor_put(struct snd_kcontrol *kcontrol,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  143. int err;
  144. if (ucontrol->value.integer.value[0] == line6pcm->volume_monitor)
  145. return 0;
  146. line6pcm->volume_monitor = ucontrol->value.integer.value[0];
  147. if (line6pcm->volume_monitor > 0) {
  148. err = line6_pcm_acquire(line6pcm, LINE6_STREAM_MONITOR, true);
  149. if (err < 0) {
  150. line6pcm->volume_monitor = 0;
  151. line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
  152. return err;
  153. }
  154. } else {
  155. line6_pcm_release(line6pcm, LINE6_STREAM_MONITOR);
  156. }
  157. return 1;
  158. }
  159. /* source info callback */
  160. static int snd_toneport_source_info(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_info *uinfo)
  162. {
  163. const int size = ARRAY_SIZE(toneport_source_info);
  164. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  165. uinfo->count = 1;
  166. uinfo->value.enumerated.items = size;
  167. if (uinfo->value.enumerated.item >= size)
  168. uinfo->value.enumerated.item = size - 1;
  169. strcpy(uinfo->value.enumerated.name,
  170. toneport_source_info[uinfo->value.enumerated.item].name);
  171. return 0;
  172. }
  173. /* source get callback */
  174. static int snd_toneport_source_get(struct snd_kcontrol *kcontrol,
  175. struct snd_ctl_elem_value *ucontrol)
  176. {
  177. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  178. struct usb_line6_toneport *toneport = line6_to_toneport(line6pcm->line6);
  179. ucontrol->value.enumerated.item[0] = toneport->source;
  180. return 0;
  181. }
  182. /* source put callback */
  183. static int snd_toneport_source_put(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
  187. struct usb_line6_toneport *toneport = line6_to_toneport(line6pcm->line6);
  188. unsigned int source;
  189. source = ucontrol->value.enumerated.item[0];
  190. if (source >= ARRAY_SIZE(toneport_source_info))
  191. return -EINVAL;
  192. if (source == toneport->source)
  193. return 0;
  194. toneport->source = source;
  195. toneport_send_cmd(toneport->line6.usbdev,
  196. toneport_source_info[source].code, 0x0000);
  197. return 1;
  198. }
  199. static void toneport_startup(struct usb_line6 *line6)
  200. {
  201. line6_pcm_acquire(line6->line6pcm, LINE6_STREAM_MONITOR, true);
  202. }
  203. /* control definition */
  204. static const struct snd_kcontrol_new toneport_control_monitor = {
  205. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  206. .name = "Monitor Playback Volume",
  207. .index = 0,
  208. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  209. .info = snd_toneport_monitor_info,
  210. .get = snd_toneport_monitor_get,
  211. .put = snd_toneport_monitor_put
  212. };
  213. /* source selector definition */
  214. static const struct snd_kcontrol_new toneport_control_source = {
  215. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  216. .name = "PCM Capture Source",
  217. .index = 0,
  218. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  219. .info = snd_toneport_source_info,
  220. .get = snd_toneport_source_get,
  221. .put = snd_toneport_source_put
  222. };
  223. /*
  224. For the led on Guitarport.
  225. Brightness goes from 0x00 to 0x26. Set a value above this to have led
  226. blink.
  227. (void cmd_0x02(byte red, byte green)
  228. */
  229. static bool toneport_has_led(struct usb_line6_toneport *toneport)
  230. {
  231. switch (toneport->type) {
  232. case LINE6_GUITARPORT:
  233. case LINE6_TONEPORT_GX:
  234. /* add your device here if you are missing support for the LEDs */
  235. return true;
  236. default:
  237. return false;
  238. }
  239. }
  240. static const char * const toneport_led_colors[2] = { "red", "green" };
  241. static const int toneport_led_init_vals[2] = { 0x00, 0x26 };
  242. static void toneport_update_led(struct usb_line6_toneport *toneport)
  243. {
  244. toneport_send_cmd(toneport->line6.usbdev,
  245. (toneport->leds[0].dev.brightness << 8) | 0x0002,
  246. toneport->leds[1].dev.brightness);
  247. }
  248. static void toneport_led_brightness_set(struct led_classdev *led_cdev,
  249. enum led_brightness brightness)
  250. {
  251. struct toneport_led *leds =
  252. container_of(led_cdev, struct toneport_led, dev);
  253. toneport_update_led(leds->toneport);
  254. }
  255. static int toneport_init_leds(struct usb_line6_toneport *toneport)
  256. {
  257. struct device *dev = &toneport->line6.usbdev->dev;
  258. int i, err;
  259. for (i = 0; i < 2; i++) {
  260. struct toneport_led *led = &toneport->leds[i];
  261. struct led_classdev *leddev = &led->dev;
  262. led->toneport = toneport;
  263. snprintf(led->name, sizeof(led->name), "%s::%s",
  264. dev_name(dev), toneport_led_colors[i]);
  265. leddev->name = led->name;
  266. leddev->brightness = toneport_led_init_vals[i];
  267. leddev->max_brightness = 0x26;
  268. leddev->brightness_set = toneport_led_brightness_set;
  269. err = led_classdev_register(dev, leddev);
  270. if (err)
  271. return err;
  272. led->registered = true;
  273. }
  274. return 0;
  275. }
  276. static void toneport_remove_leds(struct usb_line6_toneport *toneport)
  277. {
  278. struct toneport_led *led;
  279. int i;
  280. for (i = 0; i < 2; i++) {
  281. led = &toneport->leds[i];
  282. if (!led->registered)
  283. break;
  284. led_classdev_unregister(&led->dev);
  285. led->registered = false;
  286. }
  287. }
  288. static bool toneport_has_source_select(struct usb_line6_toneport *toneport)
  289. {
  290. switch (toneport->type) {
  291. case LINE6_TONEPORT_UX1:
  292. case LINE6_TONEPORT_UX2:
  293. case LINE6_PODSTUDIO_UX1:
  294. case LINE6_PODSTUDIO_UX2:
  295. return true;
  296. default:
  297. return false;
  298. }
  299. }
  300. /*
  301. Setup Toneport device.
  302. */
  303. static int toneport_setup(struct usb_line6_toneport *toneport)
  304. {
  305. u32 *ticks;
  306. struct usb_line6 *line6 = &toneport->line6;
  307. struct usb_device *usbdev = line6->usbdev;
  308. ticks = kmalloc(sizeof(*ticks), GFP_KERNEL);
  309. if (!ticks)
  310. return -ENOMEM;
  311. /* sync time on device with host: */
  312. /* note: 32-bit timestamps overflow in year 2106 */
  313. *ticks = (u32)ktime_get_real_seconds();
  314. line6_write_data(line6, 0x80c6, ticks, 4);
  315. kfree(ticks);
  316. /* enable device: */
  317. toneport_send_cmd(usbdev, 0x0301, 0x0000);
  318. /* initialize source select: */
  319. if (toneport_has_source_select(toneport))
  320. toneport_send_cmd(usbdev,
  321. toneport_source_info[toneport->source].code,
  322. 0x0000);
  323. if (toneport_has_led(toneport))
  324. toneport_update_led(toneport);
  325. schedule_delayed_work(&toneport->line6.startup_work,
  326. msecs_to_jiffies(TONEPORT_PCM_DELAY * 1000));
  327. return 0;
  328. }
  329. /*
  330. Toneport device disconnected.
  331. */
  332. static void line6_toneport_disconnect(struct usb_line6 *line6)
  333. {
  334. struct usb_line6_toneport *toneport = line6_to_toneport(line6);
  335. if (toneport_has_led(toneport))
  336. toneport_remove_leds(toneport);
  337. }
  338. /*
  339. Try to init Toneport device.
  340. */
  341. static int toneport_init(struct usb_line6 *line6,
  342. const struct usb_device_id *id)
  343. {
  344. int err;
  345. struct usb_line6_toneport *toneport = line6_to_toneport(line6);
  346. toneport->type = id->driver_info;
  347. line6->disconnect = line6_toneport_disconnect;
  348. line6->startup = toneport_startup;
  349. /* initialize PCM subsystem: */
  350. err = line6_init_pcm(line6, &toneport_pcm_properties);
  351. if (err < 0)
  352. return err;
  353. /* register monitor control: */
  354. err = snd_ctl_add(line6->card,
  355. snd_ctl_new1(&toneport_control_monitor,
  356. line6->line6pcm));
  357. if (err < 0)
  358. return err;
  359. /* register source select control: */
  360. if (toneport_has_source_select(toneport)) {
  361. err =
  362. snd_ctl_add(line6->card,
  363. snd_ctl_new1(&toneport_control_source,
  364. line6->line6pcm));
  365. if (err < 0)
  366. return err;
  367. }
  368. line6_read_serial_number(line6, &toneport->serial_number);
  369. line6_read_data(line6, 0x80c2, &toneport->firmware_version, 1);
  370. if (toneport_has_led(toneport)) {
  371. err = toneport_init_leds(toneport);
  372. if (err < 0)
  373. return err;
  374. }
  375. err = toneport_setup(toneport);
  376. if (err)
  377. return err;
  378. /* register audio system: */
  379. return snd_card_register(line6->card);
  380. }
  381. #ifdef CONFIG_PM
  382. /*
  383. Resume Toneport device after reset.
  384. */
  385. static int toneport_reset_resume(struct usb_interface *interface)
  386. {
  387. int err;
  388. err = toneport_setup(usb_get_intfdata(interface));
  389. if (err)
  390. return err;
  391. return line6_resume(interface);
  392. }
  393. #endif
  394. #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
  395. #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
  396. /* table of devices that work with this driver */
  397. static const struct usb_device_id toneport_id_table[] = {
  398. { LINE6_DEVICE(0x4750), .driver_info = LINE6_GUITARPORT },
  399. { LINE6_DEVICE(0x4153), .driver_info = LINE6_PODSTUDIO_GX },
  400. { LINE6_DEVICE(0x4150), .driver_info = LINE6_PODSTUDIO_UX1 },
  401. { LINE6_IF_NUM(0x4151, 0), .driver_info = LINE6_PODSTUDIO_UX2 },
  402. { LINE6_DEVICE(0x4147), .driver_info = LINE6_TONEPORT_GX },
  403. { LINE6_DEVICE(0x4141), .driver_info = LINE6_TONEPORT_UX1 },
  404. { LINE6_IF_NUM(0x4142, 0), .driver_info = LINE6_TONEPORT_UX2 },
  405. {}
  406. };
  407. MODULE_DEVICE_TABLE(usb, toneport_id_table);
  408. static const struct line6_properties toneport_properties_table[] = {
  409. [LINE6_GUITARPORT] = {
  410. .id = "GuitarPort",
  411. .name = "GuitarPort",
  412. .capabilities = LINE6_CAP_PCM,
  413. .altsetting = 2, /* 1..4 seem to be ok */
  414. /* no control channel */
  415. .ep_audio_r = 0x82,
  416. .ep_audio_w = 0x01,
  417. },
  418. [LINE6_PODSTUDIO_GX] = {
  419. .id = "PODStudioGX",
  420. .name = "POD Studio GX",
  421. .capabilities = LINE6_CAP_PCM,
  422. .altsetting = 2, /* 1..4 seem to be ok */
  423. /* no control channel */
  424. .ep_audio_r = 0x82,
  425. .ep_audio_w = 0x01,
  426. },
  427. [LINE6_PODSTUDIO_UX1] = {
  428. .id = "PODStudioUX1",
  429. .name = "POD Studio UX1",
  430. .capabilities = LINE6_CAP_PCM,
  431. .altsetting = 2, /* 1..4 seem to be ok */
  432. /* no control channel */
  433. .ep_audio_r = 0x82,
  434. .ep_audio_w = 0x01,
  435. },
  436. [LINE6_PODSTUDIO_UX2] = {
  437. .id = "PODStudioUX2",
  438. .name = "POD Studio UX2",
  439. .capabilities = LINE6_CAP_PCM,
  440. .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
  441. /* no control channel */
  442. .ep_audio_r = 0x82,
  443. .ep_audio_w = 0x01,
  444. },
  445. [LINE6_TONEPORT_GX] = {
  446. .id = "TonePortGX",
  447. .name = "TonePort GX",
  448. .capabilities = LINE6_CAP_PCM,
  449. .altsetting = 2, /* 1..4 seem to be ok */
  450. /* no control channel */
  451. .ep_audio_r = 0x82,
  452. .ep_audio_w = 0x01,
  453. },
  454. [LINE6_TONEPORT_UX1] = {
  455. .id = "TonePortUX1",
  456. .name = "TonePort UX1",
  457. .capabilities = LINE6_CAP_PCM,
  458. .altsetting = 2, /* 1..4 seem to be ok */
  459. /* no control channel */
  460. .ep_audio_r = 0x82,
  461. .ep_audio_w = 0x01,
  462. },
  463. [LINE6_TONEPORT_UX2] = {
  464. .id = "TonePortUX2",
  465. .name = "TonePort UX2",
  466. .capabilities = LINE6_CAP_PCM,
  467. .altsetting = 2, /* defaults to 44.1kHz, 16-bit */
  468. /* no control channel */
  469. .ep_audio_r = 0x82,
  470. .ep_audio_w = 0x01,
  471. },
  472. };
  473. /*
  474. Probe USB device.
  475. */
  476. static int toneport_probe(struct usb_interface *interface,
  477. const struct usb_device_id *id)
  478. {
  479. return line6_probe(interface, id, "Line6-TonePort",
  480. &toneport_properties_table[id->driver_info],
  481. toneport_init, sizeof(struct usb_line6_toneport));
  482. }
  483. static struct usb_driver toneport_driver = {
  484. .name = KBUILD_MODNAME,
  485. .probe = toneport_probe,
  486. .disconnect = line6_disconnect,
  487. #ifdef CONFIG_PM
  488. .suspend = line6_suspend,
  489. .resume = line6_resume,
  490. .reset_resume = toneport_reset_resume,
  491. #endif
  492. .id_table = toneport_id_table,
  493. };
  494. module_usb_driver(toneport_driver);
  495. MODULE_DESCRIPTION("TonePort USB driver");
  496. MODULE_LICENSE("GPL");