ainstr_gf1.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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(struct snd_gf1_ops *ops,
  41. struct gf1_instrument *ip,
  42. char __user **data,
  43. long *len,
  44. int atomic)
  45. {
  46. struct gf1_wave *wp, *prev;
  47. struct gf1_xwave xp;
  48. int err;
  49. gfp_t 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 = kzalloc(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(struct snd_gf1_ops *ops,
  113. struct gf1_wave *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(struct snd_gf1_ops *ops,
  121. struct gf1_instrument *ip,
  122. int atomic)
  123. {
  124. struct gf1_wave *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, struct snd_seq_kinstr *instr,
  131. char __user *instr_data, long len, int atomic,
  132. int cmd)
  133. {
  134. struct snd_gf1_ops *ops = private_data;
  135. struct gf1_instrument *ip;
  136. struct gf1_xinstrument ix;
  137. int err;
  138. gfp_t gfp_mask;
  139. if (cmd != SNDRV_SEQ_INSTR_PUT_CMD_CREATE)
  140. return -EINVAL;
  141. gfp_mask = atomic ? GFP_ATOMIC : GFP_KERNEL;
  142. /* copy instrument data */
  143. if (len < (long)sizeof(ix))
  144. return -EINVAL;
  145. if (copy_from_user(&ix, instr_data, sizeof(ix)))
  146. return -EFAULT;
  147. if (ix.stype != GF1_STRU_INSTR)
  148. return -EINVAL;
  149. instr_data += sizeof(ix);
  150. len -= sizeof(ix);
  151. ip = (struct gf1_instrument *)KINSTR_DATA(instr);
  152. ip->exclusion = le16_to_cpu(ix.exclusion);
  153. ip->exclusion_group = le16_to_cpu(ix.exclusion_group);
  154. ip->effect1 = ix.effect1;
  155. ip->effect1_depth = ix.effect1_depth;
  156. ip->effect2 = ix.effect2;
  157. ip->effect2_depth = ix.effect2_depth;
  158. /* copy layers */
  159. while (len > (long)sizeof(__u32)) {
  160. __u32 stype;
  161. if (copy_from_user(&stype, instr_data, sizeof(stype)))
  162. return -EFAULT;
  163. if (stype != GF1_STRU_WAVE) {
  164. snd_seq_gf1_instr_free(ops, ip, atomic);
  165. return -EINVAL;
  166. }
  167. err = snd_seq_gf1_copy_wave_from_stream(ops,
  168. ip,
  169. &instr_data,
  170. &len,
  171. atomic);
  172. if (err < 0) {
  173. snd_seq_gf1_instr_free(ops, ip, atomic);
  174. return err;
  175. }
  176. }
  177. return 0;
  178. }
  179. static int snd_seq_gf1_copy_wave_to_stream(struct snd_gf1_ops *ops,
  180. struct gf1_instrument *ip,
  181. char __user **data,
  182. long *len,
  183. int atomic)
  184. {
  185. struct gf1_wave *wp;
  186. struct gf1_xwave xp;
  187. int err;
  188. unsigned int real_size;
  189. for (wp = ip->wave; wp; wp = wp->next) {
  190. if (*len < (long)sizeof(xp))
  191. return -ENOMEM;
  192. memset(&xp, 0, sizeof(xp));
  193. xp.stype = GF1_STRU_WAVE;
  194. xp.share_id[0] = cpu_to_le32(wp->share_id[0]);
  195. xp.share_id[1] = cpu_to_le32(wp->share_id[1]);
  196. xp.share_id[2] = cpu_to_le32(wp->share_id[2]);
  197. xp.share_id[3] = cpu_to_le32(wp->share_id[3]);
  198. xp.format = cpu_to_le32(wp->format);
  199. xp.size = cpu_to_le32(wp->size);
  200. xp.start = cpu_to_le32(wp->start);
  201. xp.loop_start = cpu_to_le32(wp->loop_start);
  202. xp.loop_end = cpu_to_le32(wp->loop_end);
  203. xp.loop_repeat = cpu_to_le32(wp->loop_repeat);
  204. xp.flags = wp->flags;
  205. xp.sample_rate = cpu_to_le32(wp->sample_rate);
  206. xp.low_frequency = cpu_to_le32(wp->low_frequency);
  207. xp.high_frequency = cpu_to_le32(wp->high_frequency);
  208. xp.root_frequency = cpu_to_le32(wp->root_frequency);
  209. xp.tune = cpu_to_le16(wp->tune);
  210. xp.balance = wp->balance;
  211. memcpy(xp.envelope_rate, wp->envelope_rate, 6);
  212. memcpy(xp.envelope_offset, wp->envelope_offset, 6);
  213. xp.tremolo_sweep = wp->tremolo_sweep;
  214. xp.tremolo_rate = wp->tremolo_rate;
  215. xp.tremolo_depth = wp->tremolo_depth;
  216. xp.vibrato_sweep = wp->vibrato_sweep;
  217. xp.vibrato_rate = wp->vibrato_rate;
  218. xp.vibrato_depth = wp->vibrato_depth;
  219. xp.scale_frequency = cpu_to_le16(wp->scale_frequency);
  220. xp.scale_factor = cpu_to_le16(wp->scale_factor);
  221. if (copy_to_user(*data, &xp, sizeof(xp)))
  222. return -EFAULT;
  223. *data += sizeof(xp);
  224. *len -= sizeof(xp);
  225. real_size = snd_seq_gf1_size(wp->size, wp->format);
  226. if (*len < (long)real_size)
  227. return -ENOMEM;
  228. if (ops->get_sample) {
  229. err = ops->get_sample(ops->private_data, wp,
  230. *data, real_size, atomic);
  231. if (err < 0)
  232. return err;
  233. }
  234. *data += wp->size;
  235. *len -= wp->size;
  236. }
  237. return 0;
  238. }
  239. static int snd_seq_gf1_get(void *private_data, struct snd_seq_kinstr *instr,
  240. char __user *instr_data, long len, int atomic,
  241. int cmd)
  242. {
  243. struct snd_gf1_ops *ops = private_data;
  244. struct gf1_instrument *ip;
  245. struct gf1_xinstrument ix;
  246. if (cmd != SNDRV_SEQ_INSTR_GET_CMD_FULL)
  247. return -EINVAL;
  248. if (len < (long)sizeof(ix))
  249. return -ENOMEM;
  250. memset(&ix, 0, sizeof(ix));
  251. ip = (struct gf1_instrument *)KINSTR_DATA(instr);
  252. ix.stype = GF1_STRU_INSTR;
  253. ix.exclusion = cpu_to_le16(ip->exclusion);
  254. ix.exclusion_group = cpu_to_le16(ip->exclusion_group);
  255. ix.effect1 = cpu_to_le16(ip->effect1);
  256. ix.effect1_depth = cpu_to_le16(ip->effect1_depth);
  257. ix.effect2 = ip->effect2;
  258. ix.effect2_depth = ip->effect2_depth;
  259. if (copy_to_user(instr_data, &ix, sizeof(ix)))
  260. return -EFAULT;
  261. instr_data += sizeof(ix);
  262. len -= sizeof(ix);
  263. return snd_seq_gf1_copy_wave_to_stream(ops,
  264. ip,
  265. &instr_data,
  266. &len,
  267. atomic);
  268. }
  269. static int snd_seq_gf1_get_size(void *private_data, struct snd_seq_kinstr *instr,
  270. long *size)
  271. {
  272. long result;
  273. struct gf1_instrument *ip;
  274. struct gf1_wave *wp;
  275. *size = 0;
  276. ip = (struct gf1_instrument *)KINSTR_DATA(instr);
  277. result = sizeof(struct gf1_xinstrument);
  278. for (wp = ip->wave; wp; wp = wp->next) {
  279. result += sizeof(struct gf1_xwave);
  280. result += wp->size;
  281. }
  282. *size = result;
  283. return 0;
  284. }
  285. static int snd_seq_gf1_remove(void *private_data,
  286. struct snd_seq_kinstr *instr,
  287. int atomic)
  288. {
  289. struct snd_gf1_ops *ops = private_data;
  290. struct gf1_instrument *ip;
  291. ip = (struct gf1_instrument *)KINSTR_DATA(instr);
  292. snd_seq_gf1_instr_free(ops, ip, atomic);
  293. return 0;
  294. }
  295. static void snd_seq_gf1_notify(void *private_data,
  296. struct snd_seq_kinstr *instr,
  297. int what)
  298. {
  299. struct snd_gf1_ops *ops = private_data;
  300. if (ops->notify)
  301. ops->notify(ops->private_data, instr, what);
  302. }
  303. int snd_seq_gf1_init(struct snd_gf1_ops *ops,
  304. void *private_data,
  305. struct snd_seq_kinstr_ops *next)
  306. {
  307. memset(ops, 0, sizeof(*ops));
  308. ops->private_data = private_data;
  309. ops->kops.private_data = ops;
  310. ops->kops.add_len = sizeof(struct gf1_instrument);
  311. ops->kops.instr_type = SNDRV_SEQ_INSTR_ID_GUS_PATCH;
  312. ops->kops.put = snd_seq_gf1_put;
  313. ops->kops.get = snd_seq_gf1_get;
  314. ops->kops.get_size = snd_seq_gf1_get_size;
  315. ops->kops.remove = snd_seq_gf1_remove;
  316. ops->kops.notify = snd_seq_gf1_notify;
  317. ops->kops.next = next;
  318. return 0;
  319. }
  320. /*
  321. * Init part
  322. */
  323. static int __init alsa_ainstr_gf1_init(void)
  324. {
  325. return 0;
  326. }
  327. static void __exit alsa_ainstr_gf1_exit(void)
  328. {
  329. }
  330. module_init(alsa_ainstr_gf1_init)
  331. module_exit(alsa_ainstr_gf1_exit)
  332. EXPORT_SYMBOL(snd_seq_gf1_init);