variax.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/spinlock.h>
  13. #include <linux/usb.h>
  14. #include <linux/wait.h>
  15. #include <linux/module.h>
  16. #include <sound/core.h>
  17. #include "driver.h"
  18. #define VARIAX_STARTUP_DELAY1 1000
  19. #define VARIAX_STARTUP_DELAY3 100
  20. #define VARIAX_STARTUP_DELAY4 100
  21. /*
  22. Stages of Variax startup procedure
  23. */
  24. enum {
  25. VARIAX_STARTUP_INIT = 1,
  26. VARIAX_STARTUP_VERSIONREQ,
  27. VARIAX_STARTUP_WAIT,
  28. VARIAX_STARTUP_ACTIVATE,
  29. VARIAX_STARTUP_WORKQUEUE,
  30. VARIAX_STARTUP_SETUP,
  31. VARIAX_STARTUP_LAST = VARIAX_STARTUP_SETUP - 1
  32. };
  33. enum {
  34. LINE6_PODXTLIVE_VARIAX,
  35. LINE6_VARIAX
  36. };
  37. struct usb_line6_variax {
  38. /* Generic Line 6 USB data */
  39. struct usb_line6 line6;
  40. /* Buffer for activation code */
  41. unsigned char *buffer_activate;
  42. /* Handler for device initialization */
  43. struct work_struct startup_work;
  44. /* Timers for device initialization */
  45. struct timer_list startup_timer1;
  46. struct timer_list startup_timer2;
  47. /* Current progress in startup procedure */
  48. int startup_progress;
  49. };
  50. #define VARIAX_OFFSET_ACTIVATE 7
  51. /*
  52. This message is sent by the device during initialization and identifies
  53. the connected guitar version.
  54. */
  55. static const char variax_init_version[] = {
  56. 0xf0, 0x7e, 0x7f, 0x06, 0x02, 0x00, 0x01, 0x0c,
  57. 0x07, 0x00, 0x00, 0x00
  58. };
  59. /*
  60. This message is the last one sent by the device during initialization.
  61. */
  62. static const char variax_init_done[] = {
  63. 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x6b
  64. };
  65. static const char variax_activate[] = {
  66. 0xf0, 0x00, 0x01, 0x0c, 0x07, 0x00, 0x2a, 0x01,
  67. 0xf7
  68. };
  69. /* forward declarations: */
  70. static void variax_startup2(unsigned long data);
  71. static void variax_startup4(unsigned long data);
  72. static void variax_startup5(unsigned long data);
  73. static void variax_activate_async(struct usb_line6_variax *variax, int a)
  74. {
  75. variax->buffer_activate[VARIAX_OFFSET_ACTIVATE] = a;
  76. line6_send_raw_message_async(&variax->line6, variax->buffer_activate,
  77. sizeof(variax_activate));
  78. }
  79. /*
  80. Variax startup procedure.
  81. This is a sequence of functions with special requirements (e.g., must
  82. not run immediately after initialization, must not run in interrupt
  83. context). After the last one has finished, the device is ready to use.
  84. */
  85. static void variax_startup1(struct usb_line6_variax *variax)
  86. {
  87. CHECK_STARTUP_PROGRESS(variax->startup_progress, VARIAX_STARTUP_INIT);
  88. /* delay startup procedure: */
  89. line6_start_timer(&variax->startup_timer1, VARIAX_STARTUP_DELAY1,
  90. variax_startup2, (unsigned long)variax);
  91. }
  92. static void variax_startup2(unsigned long data)
  93. {
  94. struct usb_line6_variax *variax = (struct usb_line6_variax *)data;
  95. struct usb_line6 *line6 = &variax->line6;
  96. /* schedule another startup procedure until startup is complete: */
  97. if (variax->startup_progress >= VARIAX_STARTUP_LAST)
  98. return;
  99. variax->startup_progress = VARIAX_STARTUP_VERSIONREQ;
  100. line6_start_timer(&variax->startup_timer1, VARIAX_STARTUP_DELAY1,
  101. variax_startup2, (unsigned long)variax);
  102. /* request firmware version: */
  103. line6_version_request_async(line6);
  104. }
  105. static void variax_startup3(struct usb_line6_variax *variax)
  106. {
  107. CHECK_STARTUP_PROGRESS(variax->startup_progress, VARIAX_STARTUP_WAIT);
  108. /* delay startup procedure: */
  109. line6_start_timer(&variax->startup_timer2, VARIAX_STARTUP_DELAY3,
  110. variax_startup4, (unsigned long)variax);
  111. }
  112. static void variax_startup4(unsigned long data)
  113. {
  114. struct usb_line6_variax *variax = (struct usb_line6_variax *)data;
  115. CHECK_STARTUP_PROGRESS(variax->startup_progress,
  116. VARIAX_STARTUP_ACTIVATE);
  117. /* activate device: */
  118. variax_activate_async(variax, 1);
  119. line6_start_timer(&variax->startup_timer2, VARIAX_STARTUP_DELAY4,
  120. variax_startup5, (unsigned long)variax);
  121. }
  122. static void variax_startup5(unsigned long data)
  123. {
  124. struct usb_line6_variax *variax = (struct usb_line6_variax *)data;
  125. CHECK_STARTUP_PROGRESS(variax->startup_progress,
  126. VARIAX_STARTUP_WORKQUEUE);
  127. /* schedule work for global work queue: */
  128. schedule_work(&variax->startup_work);
  129. }
  130. static void variax_startup6(struct work_struct *work)
  131. {
  132. struct usb_line6_variax *variax =
  133. container_of(work, struct usb_line6_variax, startup_work);
  134. CHECK_STARTUP_PROGRESS(variax->startup_progress, VARIAX_STARTUP_SETUP);
  135. /* ALSA audio interface: */
  136. snd_card_register(variax->line6.card);
  137. }
  138. /*
  139. Process a completely received message.
  140. */
  141. static void line6_variax_process_message(struct usb_line6 *line6)
  142. {
  143. struct usb_line6_variax *variax = (struct usb_line6_variax *) line6;
  144. const unsigned char *buf = variax->line6.buffer_message;
  145. switch (buf[0]) {
  146. case LINE6_RESET:
  147. dev_info(variax->line6.ifcdev, "VARIAX reset\n");
  148. break;
  149. case LINE6_SYSEX_BEGIN:
  150. if (memcmp(buf + 1, variax_init_version + 1,
  151. sizeof(variax_init_version) - 1) == 0) {
  152. variax_startup3(variax);
  153. } else if (memcmp(buf + 1, variax_init_done + 1,
  154. sizeof(variax_init_done) - 1) == 0) {
  155. /* notify of complete initialization: */
  156. variax_startup4((unsigned long)variax);
  157. }
  158. break;
  159. }
  160. }
  161. /*
  162. Variax destructor.
  163. */
  164. static void line6_variax_disconnect(struct usb_line6 *line6)
  165. {
  166. struct usb_line6_variax *variax = (struct usb_line6_variax *)line6;
  167. del_timer(&variax->startup_timer1);
  168. del_timer(&variax->startup_timer2);
  169. cancel_work_sync(&variax->startup_work);
  170. kfree(variax->buffer_activate);
  171. }
  172. /*
  173. Try to init workbench device.
  174. */
  175. static int variax_init(struct usb_line6 *line6,
  176. const struct usb_device_id *id)
  177. {
  178. struct usb_line6_variax *variax = (struct usb_line6_variax *) line6;
  179. int err;
  180. line6->process_message = line6_variax_process_message;
  181. line6->disconnect = line6_variax_disconnect;
  182. init_timer(&variax->startup_timer1);
  183. init_timer(&variax->startup_timer2);
  184. INIT_WORK(&variax->startup_work, variax_startup6);
  185. /* initialize USB buffers: */
  186. variax->buffer_activate = kmemdup(variax_activate,
  187. sizeof(variax_activate), GFP_KERNEL);
  188. if (variax->buffer_activate == NULL)
  189. return -ENOMEM;
  190. /* initialize MIDI subsystem: */
  191. err = line6_init_midi(&variax->line6);
  192. if (err < 0)
  193. return err;
  194. /* initiate startup procedure: */
  195. variax_startup1(variax);
  196. return 0;
  197. }
  198. #define LINE6_DEVICE(prod) USB_DEVICE(0x0e41, prod)
  199. #define LINE6_IF_NUM(prod, n) USB_DEVICE_INTERFACE_NUMBER(0x0e41, prod, n)
  200. /* table of devices that work with this driver */
  201. static const struct usb_device_id variax_id_table[] = {
  202. { LINE6_IF_NUM(0x4650, 1), .driver_info = LINE6_PODXTLIVE_VARIAX },
  203. { LINE6_DEVICE(0x534d), .driver_info = LINE6_VARIAX },
  204. {}
  205. };
  206. MODULE_DEVICE_TABLE(usb, variax_id_table);
  207. static const struct line6_properties variax_properties_table[] = {
  208. [LINE6_PODXTLIVE_VARIAX] = {
  209. .id = "PODxtLive",
  210. .name = "PODxt Live",
  211. .capabilities = LINE6_CAP_CONTROL,
  212. .altsetting = 1,
  213. .ep_ctrl_r = 0x86,
  214. .ep_ctrl_w = 0x05,
  215. .ep_audio_r = 0x82,
  216. .ep_audio_w = 0x01,
  217. },
  218. [LINE6_VARIAX] = {
  219. .id = "Variax",
  220. .name = "Variax Workbench",
  221. .capabilities = LINE6_CAP_CONTROL,
  222. .altsetting = 1,
  223. .ep_ctrl_r = 0x82,
  224. .ep_ctrl_w = 0x01,
  225. /* no audio channel */
  226. }
  227. };
  228. /*
  229. Probe USB device.
  230. */
  231. static int variax_probe(struct usb_interface *interface,
  232. const struct usb_device_id *id)
  233. {
  234. return line6_probe(interface, id, "Line6-Variax",
  235. &variax_properties_table[id->driver_info],
  236. variax_init, sizeof(struct usb_line6_variax));
  237. }
  238. static struct usb_driver variax_driver = {
  239. .name = KBUILD_MODNAME,
  240. .probe = variax_probe,
  241. .disconnect = line6_disconnect,
  242. #ifdef CONFIG_PM
  243. .suspend = line6_suspend,
  244. .resume = line6_resume,
  245. .reset_resume = line6_resume,
  246. #endif
  247. .id_table = variax_id_table,
  248. };
  249. module_usb_driver(variax_driver);
  250. MODULE_DESCRIPTION("Vairax Workbench USB driver");
  251. MODULE_LICENSE("GPL");