pod.c 14 KB

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