toneport.c 14 KB

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