ainstr_gf1.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * GF1 (GUS) Patch - Instrument routines
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <sound/core.h>
  25. #include <sound/ainstr_gf1.h>
  26. #include <sound/initval.h>
  27. #include <asm/uaccess.h>
  28. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  29. MODULE_DESCRIPTION("Advanced Linux Sound Architecture GF1 (GUS) Patch support.");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int snd_seq_gf1_size(unsigned int size, unsigned int format)
  32. {
  33. unsigned int result = size;
  34. if (format & GF1_WAVE_16BIT)
  35. result <<= 1;
  36. if (format & GF1_WAVE_STEREO)
  37. result <<= 1;
  38. return format;
  39. }
  40. static int snd_seq_gf1_copy_wave_from_stream(snd_gf1_ops_t *ops,
  41. gf1_instrument_t *ip,
  42. char __user **data,
  43. long *len,
  44. int atomic)
  45. {
  46. gf1_wave_t *wp, *prev;
  47. gf1_xwave_t xp;
  48. int err, gfp_mask;
  49. unsigned int real_size;
  50. gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
  51. if (*len < (long)sizeof(xp))
  52. return -EINVAL;
  53. if (copy_from_user(&xp, *data, sizeof(xp)))
  54. return -EFAULT;
  55. *data += sizeof(xp);
  56. *len -= sizeof(xp);
  57. wp = kcalloc(1, sizeof(*wp), gfp_mask);
  58. if (wp == NULL)
  59. return -ENOMEM;
  60. wp->share_id[0] = le32_to_cpu(xp.share_id[0]);
  61. wp->share_id[1] = le32_to_cpu(xp.share_id[1]);
  62. wp->share_id[2] = le32_to_cpu(xp.share_id[2]);
  63. wp->share_id[3] = le32_to_cpu(xp.share_id[3]);
  64. wp->format = le32_to_cpu(xp.format);
  65. wp->size = le32_to_cpu(xp.size);
  66. wp->start = le32_to_cpu(xp.start);
  67. wp->loop_start = le32_to_cpu(xp.loop_start);
  68. wp->loop_end = le32_to_cpu(xp.loop_end);
  69. wp->loop_repeat = le16_to_cpu(xp.loop_repeat);
  70. wp->flags = xp.flags;
  71. wp->sample_rate = le32_to_cpu(xp.sample_rate);
  72. wp->low_frequency = le32_to_cpu(xp.low_frequency);
  73. wp->high_frequency = le32_to_cpu(xp.high_frequency);
  74. wp->root_frequency = le32_to_cpu(xp.root_frequency);
  75. wp->tune = le16_to_cpu(xp.tune);
  76. wp->balance = xp.balance;
  77. memcpy(wp->envelope_rate, xp.envelope_rate, 6);
  78. memcpy(wp->envelope_offset, xp.envelope_offset, 6);
  79. wp->tremolo_sweep = xp.tremolo_sweep;
  80. wp->tremolo_rate = xp.tremolo_rate;
  81. wp->tremolo_depth = xp.tremolo_depth;
  82. wp->vibrato_sweep = xp.vibrato_sweep;
  83. wp->vibrato_rate = xp.vibrato_rate;
  84. wp->vibrato_depth = xp.vibrato_depth;
  85. wp->scale_frequency = le16_to_cpu(xp.scale_frequency);
  86. wp->scale_factor = le16_to_cpu(xp.scale_factor);
  87. real_size = snd_seq_gf1_size(wp->size, wp->format);
  88. if ((long)real_size > *len) {
  89. kfree(wp);
  90. return -ENOMEM;
  91. }
  92. if (ops->put_sample) {
  93. err = ops->put_sample(ops->private_data, wp,
  94. *data, real_size, atomic);
  95. if (err < 0) {
  96. kfree(wp);
  97. return err;
  98. }
  99. }
  100. *data += real_size;
  101. *len -= real_size;
  102. prev = ip->wave;
  103. if (prev) {
  104. while (prev->next) prev = prev->next;
  105. prev->next = wp;
  106. } else {
  107. ip->wave = wp;
  108. }
  109. return 0;
  110. }
  111. static void snd_seq_gf1_wave_free(snd_gf1_ops_t *ops,
  112. gf1_wave_t *wave,
  113. int atomic)
  114. {
  115. if (ops->remove_sample)
  116. ops->remove_sample(ops->private_data, wave, atomic);
  117. kfree(wave);
  118. }
  119. static void snd_seq_gf1_instr_free(snd_gf1_ops_t *ops,
  120. gf1_instrument_t *ip,
  121. int atomic)
  122. {
  123. gf1_wave_t *wave;
  124. while ((wave = ip->wave) != NULL) {
  125. ip->wave = wave->next;
  126. snd_seq_gf1_wave_free(ops, wave, atomic);
  127. }
  128. }
  129. static int snd_seq_gf1_put(void *private_data, snd_seq_kinstr_t *instr,
  130. char __user *instr_data, long len, int atomic,
  131. int cmd)
  132. {
  133. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  134. gf1_instrument_t *ip;
  135. gf1_xinstrument_t ix;
  136. int err, gfp_mask;
  137. if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
  138. return -EINVAL;
  139. gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
  140. /* copy instrument data */
  141. if (len < (long)sizeof(ix))
  142. return -EINVAL;
  143. if (copy_from_user(&ix, instr_data, sizeof(ix)))
  144. return -EFAULT;
  145. if (ix.stype != GF1_STRU_INSTR)
  146. return -EINVAL;
  147. instr_data += sizeof(ix);
  148. len -= sizeof(ix);
  149. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  150. ip->exclusion = le16_to_cpu(ix.exclusion);
  151. ip->exclusion_group = le16_to_cpu(ix.exclusion_group);
  152. ip->effect1 = ix.effect1;
  153. ip->effect1_depth = ix.effect1_depth;
  154. ip->effect2 = ix.effect2;
  155. ip->effect2_depth = ix.effect2_depth;
  156. /* copy layers */
  157. while (len > (long)sizeof(__u32)) {
  158. __u32 stype;
  159. if (copy_from_user(&stype, instr_data, sizeof(stype)))
  160. return -EFAULT;
  161. if (stype != GF1_STRU_WAVE) {
  162. snd_seq_gf1_instr_free(ops, ip, atomic);
  163. return -EINVAL;
  164. }
  165. err = snd_seq_gf1_copy_wave_from_stream(ops,
  166. ip,
  167. &instr_data,
  168. &len,
  169. atomic);
  170. if (err < 0) {
  171. snd_seq_gf1_instr_free(ops, ip, atomic);
  172. return err;
  173. }
  174. }
  175. return 0;
  176. }
  177. static int snd_seq_gf1_copy_wave_to_stream(snd_gf1_ops_t *ops,
  178. gf1_instrument_t *ip,
  179. char __user **data,
  180. long *len,
  181. int atomic)
  182. {
  183. gf1_wave_t *wp;
  184. gf1_xwave_t xp;
  185. int err;
  186. unsigned int real_size;
  187. for (wp = ip->wave; wp; wp = wp->next) {
  188. if (*len < (long)sizeof(xp))
  189. return -ENOMEM;
  190. memset(&xp, 0, sizeof(xp));
  191. xp.stype = GF1_STRU_WAVE;
  192. xp.share_id[0] = cpu_to_le32(wp->share_id[0]);
  193. xp.share_id[1] = cpu_to_le32(wp->share_id[1]);
  194. xp.share_id[2] = cpu_to_le32(wp->share_id[2]);
  195. xp.share_id[3] = cpu_to_le32(wp->share_id[3]);
  196. xp.format = cpu_to_le32(wp->format);
  197. xp.size = cpu_to_le32(wp->size);
  198. xp.start = cpu_to_le32(wp->start);
  199. xp.loop_start = cpu_to_le32(wp->loop_start);
  200. xp.loop_end = cpu_to_le32(wp->loop_end);
  201. xp.loop_repeat = cpu_to_le32(wp->loop_repeat);
  202. xp.flags = wp->flags;
  203. xp.sample_rate = cpu_to_le32(wp->sample_rate);
  204. xp.low_frequency = cpu_to_le32(wp->low_frequency);
  205. xp.high_frequency = cpu_to_le32(wp->high_frequency);
  206. xp.root_frequency = cpu_to_le32(wp->root_frequency);
  207. xp.tune = cpu_to_le16(wp->tune);
  208. xp.balance = wp->balance;
  209. memcpy(xp.envelope_rate, wp->envelope_rate, 6);
  210. memcpy(xp.envelope_offset, wp->envelope_offset, 6);
  211. xp.tremolo_sweep = wp->tremolo_sweep;
  212. xp.tremolo_rate = wp->tremolo_rate;
  213. xp.tremolo_depth = wp->tremolo_depth;
  214. xp.vibrato_sweep = wp->vibrato_sweep;
  215. xp.vibrato_rate = wp->vibrato_rate;
  216. xp.vibrato_depth = wp->vibrato_depth;
  217. xp.scale_frequency = cpu_to_le16(wp->scale_frequency);
  218. xp.scale_factor = cpu_to_le16(wp->scale_factor);
  219. if (copy_to_user(*data, &xp, sizeof(xp)))
  220. return -EFAULT;
  221. *data += sizeof(xp);
  222. *len -= sizeof(xp);
  223. real_size = snd_seq_gf1_size(wp->size, wp->format);
  224. if (*len < (long)real_size)
  225. return -ENOMEM;
  226. if (ops->get_sample) {
  227. err = ops->get_sample(ops->private_data, wp,
  228. *data, real_size, atomic);
  229. if (err < 0)
  230. return err;
  231. }
  232. *data += wp->size;
  233. *len -= wp->size;
  234. }
  235. return 0;
  236. }
  237. static int snd_seq_gf1_get(void *private_data, snd_seq_kinstr_t *instr,
  238. char __user *instr_data, long len, int atomic,
  239. int cmd)
  240. {
  241. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  242. gf1_instrument_t *ip;
  243. gf1_xinstrument_t ix;
  244. if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
  245. return -EINVAL;
  246. if (len < (long)sizeof(ix))
  247. return -ENOMEM;
  248. memset(&ix, 0, sizeof(ix));
  249. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  250. ix.stype = GF1_STRU_INSTR;
  251. ix.exclusion = cpu_to_le16(ip->exclusion);
  252. ix.exclusion_group = cpu_to_le16(ip->exclusion_group);
  253. ix.effect1 = cpu_to_le16(ip->effect1);
  254. ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
  255. ix.effect2 = ip->effect2;
  256. ix.effect2_depth = ip->effect2_depth;
  257. if (copy_to_user(instr_data, &ix, sizeof(ix)))
  258. return -EFAULT;
  259. instr_data += sizeof(ix);
  260. len -= sizeof(ix);
  261. return snd_seq_gf1_copy_wave_to_stream(ops,
  262. ip,
  263. &instr_data,
  264. &len,
  265. atomic);
  266. }
  267. static int snd_seq_gf1_get_size(void *private_data, snd_seq_kinstr_t *instr,
  268. long *size)
  269. {
  270. long result;
  271. gf1_instrument_t *ip;
  272. gf1_wave_t *wp;
  273. *size = 0;
  274. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  275. result = sizeof(gf1_xinstrument_t);
  276. for (wp = ip->wave; wp; wp = wp->next) {
  277. result += sizeof(gf1_xwave_t);
  278. result += wp->size;
  279. }
  280. *size = result;
  281. return 0;
  282. }
  283. static int snd_seq_gf1_remove(void *private_data,
  284. snd_seq_kinstr_t *instr,
  285. int atomic)
  286. {
  287. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  288. gf1_instrument_t *ip;
  289. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  290. snd_seq_gf1_instr_free(ops, ip, atomic);
  291. return 0;
  292. }
  293. static void snd_seq_gf1_notify(void *private_data,
  294. snd_seq_kinstr_t *instr,
  295. int what)
  296. {
  297. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  298. if (ops->notify)
  299. ops->notify(ops->private_data, instr, what);
  300. }
  301. int snd_seq_gf1_init(snd_gf1_ops_t *ops,
  302. void *private_data,
  303. snd_seq_kinstr_ops_t *next)
  304. {
  305. memset(ops, 0, sizeof(*ops));
  306. ops->private_data = private_data;
  307. ops->kops.private_data = ops;
  308. ops->kops.add_len = sizeof(gf1_instrument_t);
  309. ops->kops.instr_type = SNDRV_SEQ_INSTR_ID_GUS_PATCH;
  310. ops->kops.put = snd_seq_gf1_put;
  311. ops->kops.get = snd_seq_gf1_get;
  312. ops->kops.get_size = snd_seq_gf1_get_size;
  313. ops->kops.remove = snd_seq_gf1_remove;
  314. ops->kops.notify = snd_seq_gf1_notify;
  315. ops->kops.next = next;
  316. return 0;
  317. }
  318. /*
  319. * Init part
  320. */
  321. static int __init alsa_ainstr_gf1_init(void)
  322. {
  323. return 0;
  324. }
  325. static void __exit alsa_ainstr_gf1_exit(void)
  326. {
  327. }
  328. module_init(alsa_ainstr_gf1_init)
  329. module_exit(alsa_ainstr_gf1_exit)
  330. EXPORT_SYMBOL(snd_seq_gf1_init);