ainstr_gf1.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * GF1 (GUS) Patch - Instrument routines
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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/slab.h>
  23. #include <sound/core.h>
  24. #include <sound/ainstr_gf1.h>
  25. #include <sound/initval.h>
  26. #include <asm/uaccess.h>
  27. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  28. MODULE_DESCRIPTION("Advanced Linux Sound Architecture GF1 (GUS) Patch support.");
  29. MODULE_LICENSE("GPL");
  30. static unsigned int snd_seq_gf1_size(unsigned int size, unsigned int format)
  31. {
  32. unsigned int result = size;
  33. if (format & GF1_WAVE_16BIT)
  34. result <<= 1;
  35. if (format & GF1_WAVE_STEREO)
  36. result <<= 1;
  37. return format;
  38. }
  39. static int snd_seq_gf1_copy_wave_from_stream(struct snd_gf1_ops *ops,
  40. struct gf1_instrument *ip,
  41. char __user **data,
  42. long *len,
  43. int atomic)
  44. {
  45. struct gf1_wave *wp, *prev;
  46. struct gf1_xwave xp;
  47. int err;
  48. gfp_t 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 = kzalloc(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(struct snd_gf1_ops *ops,
  112. struct gf1_wave *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(struct snd_gf1_ops *ops,
  120. struct gf1_instrument *ip,
  121. int atomic)
  122. {
  123. struct gf1_wave *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, struct snd_seq_kinstr *instr,
  130. char __user *instr_data, long len, int atomic,
  131. int cmd)
  132. {
  133. struct snd_gf1_ops *ops = private_data;
  134. struct gf1_instrument *ip;
  135. struct gf1_xinstrument ix;
  136. int err;
  137. gfp_t 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 = (struct gf1_instrument *)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(struct snd_gf1_ops *ops,
  179. struct gf1_instrument *ip,
  180. char __user **data,
  181. long *len,
  182. int atomic)
  183. {
  184. struct gf1_wave *wp;
  185. struct gf1_xwave 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, struct snd_seq_kinstr *instr,
  239. char __user *instr_data, long len, int atomic,
  240. int cmd)
  241. {
  242. struct snd_gf1_ops *ops = private_data;
  243. struct gf1_instrument *ip;
  244. struct gf1_xinstrument 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 = (struct gf1_instrument *)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, struct snd_seq_kinstr *instr,
  269. long *size)
  270. {
  271. long result;
  272. struct gf1_instrument *ip;
  273. struct gf1_wave *wp;
  274. *size = 0;
  275. ip = (struct gf1_instrument *)KINSTR_DATA(instr);
  276. result = sizeof(struct gf1_xinstrument);
  277. for (wp = ip->wave; wp; wp = wp->next) {
  278. result += sizeof(struct gf1_xwave);
  279. result += wp->size;
  280. }
  281. *size = result;
  282. return 0;
  283. }
  284. static int snd_seq_gf1_remove(void *private_data,
  285. struct snd_seq_kinstr *instr,
  286. int atomic)
  287. {
  288. struct snd_gf1_ops *ops = private_data;
  289. struct gf1_instrument *ip;
  290. ip = (struct gf1_instrument *)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. struct snd_seq_kinstr *instr,
  296. int what)
  297. {
  298. struct snd_gf1_ops *ops = private_data;
  299. if (ops->notify)
  300. ops->notify(ops->private_data, instr, what);
  301. }
  302. int snd_seq_gf1_init(struct snd_gf1_ops *ops,
  303. void *private_data,
  304. struct snd_seq_kinstr_ops *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(struct gf1_instrument);
  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);