toneport.c 14 KB

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