ainstr_gf1.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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;
  49. unsigned int gfp_mask;
  50. unsigned int real_size;
  51. gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
  52. if (*len < (long)sizeof(xp))
  53. return -EINVAL;
  54. if (copy_from_user(&xp, *data, sizeof(xp)))
  55. return -EFAULT;
  56. *data += sizeof(xp);
  57. *len -= sizeof(xp);
  58. wp = kcalloc(1, sizeof(*wp), gfp_mask);
  59. if (wp == NULL)
  60. return -ENOMEM;
  61. wp->share_id[0] = le32_to_cpu(xp.share_id[0]);
  62. wp->share_id[1] = le32_to_cpu(xp.share_id[1]);
  63. wp->share_id[2] = le32_to_cpu(xp.share_id[2]);
  64. wp->share_id[3] = le32_to_cpu(xp.share_id[3]);
  65. wp->format = le32_to_cpu(xp.format);
  66. wp->size = le32_to_cpu(xp.size);
  67. wp->start = le32_to_cpu(xp.start);
  68. wp->loop_start = le32_to_cpu(xp.loop_start);
  69. wp->loop_end = le32_to_cpu(xp.loop_end);
  70. wp->loop_repeat = le16_to_cpu(xp.loop_repeat);
  71. wp->flags = xp.flags;
  72. wp->sample_rate = le32_to_cpu(xp.sample_rate);
  73. wp->low_frequency = le32_to_cpu(xp.low_frequency);
  74. wp->high_frequency = le32_to_cpu(xp.high_frequency);
  75. wp->root_frequency = le32_to_cpu(xp.root_frequency);
  76. wp->tune = le16_to_cpu(xp.tune);
  77. wp->balance = xp.balance;
  78. memcpy(wp->envelope_rate, xp.envelope_rate, 6);
  79. memcpy(wp->envelope_offset, xp.envelope_offset, 6);
  80. wp->tremolo_sweep = xp.tremolo_sweep;
  81. wp->tremolo_rate = xp.tremolo_rate;
  82. wp->tremolo_depth = xp.tremolo_depth;
  83. wp->vibrato_sweep = xp.vibrato_sweep;
  84. wp->vibrato_rate = xp.vibrato_rate;
  85. wp->vibrato_depth = xp.vibrato_depth;
  86. wp->scale_frequency = le16_to_cpu(xp.scale_frequency);
  87. wp->scale_factor = le16_to_cpu(xp.scale_factor);
  88. real_size = snd_seq_gf1_size(wp->size, wp->format);
  89. if ((long)real_size > *len) {
  90. kfree(wp);
  91. return -ENOMEM;
  92. }
  93. if (ops->put_sample) {
  94. err = ops->put_sample(ops->private_data, wp,
  95. *data, real_size, atomic);
  96. if (err < 0) {
  97. kfree(wp);
  98. return err;
  99. }
  100. }
  101. *data += real_size;
  102. *len -= real_size;
  103. prev = ip->wave;
  104. if (prev) {
  105. while (prev->next) prev = prev->next;
  106. prev->next = wp;
  107. } else {
  108. ip->wave = wp;
  109. }
  110. return 0;
  111. }
  112. static void snd_seq_gf1_wave_free(snd_gf1_ops_t *ops,
  113. gf1_wave_t *wave,
  114. int atomic)
  115. {
  116. if (ops->remove_sample)
  117. ops->remove_sample(ops->private_data, wave, atomic);
  118. kfree(wave);
  119. }
  120. static void snd_seq_gf1_instr_free(snd_gf1_ops_t *ops,
  121. gf1_instrument_t *ip,
  122. int atomic)
  123. {
  124. gf1_wave_t *wave;
  125. while ((wave = ip->wave) != NULL) {
  126. ip->wave = wave->next;
  127. snd_seq_gf1_wave_free(ops, wave, atomic);
  128. }
  129. }
  130. static int snd_seq_gf1_put(void *private_data, snd_seq_kinstr_t *instr,
  131. char __user *instr_data, long len, int atomic,
  132. int cmd)
  133. {
  134. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  135. gf1_instrument_t *ip;
  136. gf1_xinstrument_t ix;
  137. int err, gfp_mask;
  138. if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
  139. return -EINVAL;
  140. gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
  141. /* copy instrument data */
  142. if (len < (long)sizeof(ix))
  143. return -EINVAL;
  144. if (copy_from_user(&ix, instr_data, sizeof(ix)))
  145. return -EFAULT;
  146. if (ix.stype != GF1_STRU_INSTR)
  147. return -EINVAL;
  148. instr_data += sizeof(ix);
  149. len -= sizeof(ix);
  150. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  151. ip->exclusion = le16_to_cpu(ix.exclusion);
  152. ip->exclusion_group = le16_to_cpu(ix.exclusion_group);
  153. ip->effect1 = ix.effect1;
  154. ip->effect1_depth = ix.effect1_depth;
  155. ip->effect2 = ix.effect2;
  156. ip->effect2_depth = ix.effect2_depth;
  157. /* copy layers */
  158. while (len > (long)sizeof(__u32)) {
  159. __u32 stype;
  160. if (copy_from_user(&stype, instr_data, sizeof(stype)))
  161. return -EFAULT;
  162. if (stype != GF1_STRU_WAVE) {
  163. snd_seq_gf1_instr_free(ops, ip, atomic);
  164. return -EINVAL;
  165. }
  166. err = snd_seq_gf1_copy_wave_from_stream(ops,
  167. ip,
  168. &instr_data,
  169. &len,
  170. atomic);
  171. if (err < 0) {
  172. snd_seq_gf1_instr_free(ops, ip, atomic);
  173. return err;
  174. }
  175. }
  176. return 0;
  177. }
  178. static int snd_seq_gf1_copy_wave_to_stream(snd_gf1_ops_t *ops,
  179. gf1_instrument_t *ip,
  180. char __user **data,
  181. long *len,
  182. int atomic)
  183. {
  184. gf1_wave_t *wp;
  185. gf1_xwave_t xp;
  186. int err;
  187. unsigned int real_size;
  188. for (wp = ip->wave; wp; wp = wp->next) {
  189. if (*len < (long)sizeof(xp))
  190. return -ENOMEM;
  191. memset(&xp, 0, sizeof(xp));
  192. xp.stype = GF1_STRU_WAVE;
  193. xp.share_id[0] = cpu_to_le32(wp->share_id[0]);
  194. xp.share_id[1] = cpu_to_le32(wp->share_id[1]);
  195. xp.share_id[2] = cpu_to_le32(wp->share_id[2]);
  196. xp.share_id[3] = cpu_to_le32(wp->share_id[3]);
  197. xp.format = cpu_to_le32(wp->format);
  198. xp.size = cpu_to_le32(wp->size);
  199. xp.start = cpu_to_le32(wp->start);
  200. xp.loop_start = cpu_to_le32(wp->loop_start);
  201. xp.loop_end = cpu_to_le32(wp->loop_end);
  202. xp.loop_repeat = cpu_to_le32(wp->loop_repeat);
  203. xp.flags = wp->flags;
  204. xp.sample_rate = cpu_to_le32(wp->sample_rate);
  205. xp.low_frequency = cpu_to_le32(wp->low_frequency);
  206. xp.high_frequency = cpu_to_le32(wp->high_frequency);
  207. xp.root_frequency = cpu_to_le32(wp->root_frequency);
  208. xp.tune = cpu_to_le16(wp->tune);
  209. xp.balance = wp->balance;
  210. memcpy(xp.envelope_rate, wp->envelope_rate, 6);
  211. memcpy(xp.envelope_offset, wp->envelope_offset, 6);
  212. xp.tremolo_sweep = wp->tremolo_sweep;
  213. xp.tremolo_rate = wp->tremolo_rate;
  214. xp.tremolo_depth = wp->tremolo_depth;
  215. xp.vibrato_sweep = wp->vibrato_sweep;
  216. xp.vibrato_rate = wp->vibrato_rate;
  217. xp.vibrato_depth = wp->vibrato_depth;
  218. xp.scale_frequency = cpu_to_le16(wp->scale_frequency);
  219. xp.scale_factor = cpu_to_le16(wp->scale_factor);
  220. if (copy_to_user(*data, &xp, sizeof(xp)))
  221. return -EFAULT;
  222. *data += sizeof(xp);
  223. *len -= sizeof(xp);
  224. real_size = snd_seq_gf1_size(wp->size, wp->format);
  225. if (*len < (long)real_size)
  226. return -ENOMEM;
  227. if (ops->get_sample) {
  228. err = ops->get_sample(ops->private_data, wp,
  229. *data, real_size, atomic);
  230. if (err < 0)
  231. return err;
  232. }
  233. *data += wp->size;
  234. *len -= wp->size;
  235. }
  236. return 0;
  237. }
  238. static int snd_seq_gf1_get(void *private_data, snd_seq_kinstr_t *instr,
  239. char __user *instr_data, long len, int atomic,
  240. int cmd)
  241. {
  242. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  243. gf1_instrument_t *ip;
  244. gf1_xinstrument_t ix;
  245. if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
  246. return -EINVAL;
  247. if (len < (long)sizeof(ix))
  248. return -ENOMEM;
  249. memset(&ix, 0, sizeof(ix));
  250. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  251. ix.stype = GF1_STRU_INSTR;
  252. ix.exclusion = cpu_to_le16(ip->exclusion);
  253. ix.exclusion_group = cpu_to_le16(ip->exclusion_group);
  254. ix.effect1 = cpu_to_le16(ip->effect1);
  255. ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
  256. ix.effect2 = ip->effect2;
  257. ix.effect2_depth = ip->effect2_depth;
  258. if (copy_to_user(instr_data, &ix, sizeof(ix)))
  259. return -EFAULT;
  260. instr_data += sizeof(ix);
  261. len -= sizeof(ix);
  262. return snd_seq_gf1_copy_wave_to_stream(ops,
  263. ip,
  264. &instr_data,
  265. &len,
  266. atomic);
  267. }
  268. static int snd_seq_gf1_get_size(void *private_data, snd_seq_kinstr_t *instr,
  269. long *size)
  270. {
  271. long result;
  272. gf1_instrument_t *ip;
  273. gf1_wave_t *wp;
  274. *size = 0;
  275. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  276. result = sizeof(gf1_xinstrument_t);
  277. for (wp = ip->wave; wp; wp = wp->next) {
  278. result += sizeof(gf1_xwave_t);
  279. result += wp->size;
  280. }
  281. *size = result;
  282. return 0;
  283. }
  284. static int snd_seq_gf1_remove(void *private_data,
  285. snd_seq_kinstr_t *instr,
  286. int atomic)
  287. {
  288. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  289. gf1_instrument_t *ip;
  290. ip = (gf1_instrument_t *)KINSTR_DATA(instr);
  291. snd_seq_gf1_instr_free(ops, ip, atomic);
  292. return 0;
  293. }
  294. static void snd_seq_gf1_notify(void *private_data,
  295. snd_seq_kinstr_t *instr,
  296. int what)
  297. {
  298. snd_gf1_ops_t *ops = (snd_gf1_ops_t *)private_data;
  299. if (ops->notify)
  300. ops->notify(ops->private_data, instr, what);
  301. }
  302. int snd_seq_gf1_init(snd_gf1_ops_t *ops,
  303. void *private_data,
  304. snd_seq_kinstr_ops_t *next)
  305. {
  306. memset(ops, 0, sizeof(*ops));
  307. ops->private_data = private_data;
  308. ops->kops.private_data = ops;
  309. ops->kops.add_len = sizeof(gf1_instrument_t);
  310. ops->kops.instr_type = SNDRV_SEQ_INSTR_ID_GUS_PATCH;
  311. ops->kops.put = snd_seq_gf1_put;
  312. ops->kops.get = snd_seq_gf1_get;
  313. ops->kops.get_size = snd_seq_gf1_get_size;
  314. ops->kops.remove = snd_seq_gf1_remove;
  315. ops->kops.notify = snd_seq_gf1_notify;
  316. ops->kops.next = next;
  317. return 0;
  318. }
  319. /*
  320. * Init part
  321. */
  322. static int __init alsa_ainstr_gf1_init(void)
  323. {
  324. return 0;
  325. }
  326. static void __exit alsa_ainstr_gf1_exit(void)
  327. {
  328. }
  329. module_init(alsa_ainstr_gf1_init)
  330. module_exit(alsa_ainstr_gf1_exit)
  331. EXPORT_SYMBOL(snd_seq_gf1_init);