control_compat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * compat ioctls for control API
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* this file included from control.c */
  21. #include <linux/compat.h>
  22. struct snd_ctl_elem_list32 {
  23. u32 offset;
  24. u32 space;
  25. u32 used;
  26. u32 count;
  27. u32 pids;
  28. unsigned char reserved[50];
  29. } /* don't set packed attribute here */;
  30. static int snd_ctl_elem_list_compat(struct snd_card *card,
  31. struct snd_ctl_elem_list32 __user *data32)
  32. {
  33. struct snd_ctl_elem_list __user *data;
  34. compat_caddr_t ptr;
  35. int err;
  36. data = compat_alloc_user_space(sizeof(*data));
  37. /* offset, space, used, count */
  38. if (copy_in_user(data, data32, 4 * sizeof(u32)))
  39. return -EFAULT;
  40. /* pids */
  41. if (get_user(ptr, &data32->pids) ||
  42. put_user(compat_ptr(ptr), &data->pids))
  43. return -EFAULT;
  44. err = snd_ctl_elem_list(card, data);
  45. if (err < 0)
  46. return err;
  47. /* copy the result */
  48. if (copy_in_user(data32, data, 4 * sizeof(u32)))
  49. return -EFAULT;
  50. return 0;
  51. }
  52. /*
  53. * control element info
  54. * it uses union, so the things are not easy..
  55. */
  56. struct snd_ctl_elem_info32 {
  57. struct snd_ctl_elem_id id; // the size of struct is same
  58. s32 type;
  59. u32 access;
  60. u32 count;
  61. s32 owner;
  62. union {
  63. struct {
  64. s32 min;
  65. s32 max;
  66. s32 step;
  67. } integer;
  68. struct {
  69. u64 min;
  70. u64 max;
  71. u64 step;
  72. } integer64;
  73. struct {
  74. u32 items;
  75. u32 item;
  76. char name[64];
  77. } enumerated;
  78. unsigned char reserved[128];
  79. } value;
  80. unsigned char reserved[64];
  81. } __attribute__((packed));
  82. static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
  83. struct snd_ctl_elem_info32 __user *data32)
  84. {
  85. struct snd_ctl_elem_info *data;
  86. int err;
  87. data = kzalloc(sizeof(*data), GFP_KERNEL);
  88. if (! data)
  89. return -ENOMEM;
  90. err = -EFAULT;
  91. /* copy id */
  92. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  93. goto error;
  94. /* we need to copy the item index.
  95. * hope this doesn't break anything..
  96. */
  97. if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
  98. goto error;
  99. err = snd_ctl_elem_info(ctl, data);
  100. if (err < 0)
  101. goto error;
  102. /* restore info to 32bit */
  103. err = -EFAULT;
  104. /* id, type, access, count */
  105. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  106. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  107. goto error;
  108. if (put_user(data->owner, &data32->owner))
  109. goto error;
  110. switch (data->type) {
  111. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  112. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  113. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  114. put_user(data->value.integer.max, &data32->value.integer.max) ||
  115. put_user(data->value.integer.step, &data32->value.integer.step))
  116. goto error;
  117. break;
  118. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  119. if (copy_to_user(&data32->value.integer64,
  120. &data->value.integer64,
  121. sizeof(data->value.integer64)))
  122. goto error;
  123. break;
  124. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  125. if (copy_to_user(&data32->value.enumerated,
  126. &data->value.enumerated,
  127. sizeof(data->value.enumerated)))
  128. goto error;
  129. break;
  130. default:
  131. break;
  132. }
  133. err = 0;
  134. error:
  135. kfree(data);
  136. return err;
  137. }
  138. /* read / write */
  139. struct snd_ctl_elem_value32 {
  140. struct snd_ctl_elem_id id;
  141. unsigned int indirect; /* bit-field causes misalignment */
  142. union {
  143. s32 integer[128];
  144. unsigned char data[512];
  145. #ifndef CONFIG_X86_64
  146. s64 integer64[64];
  147. #endif
  148. } value;
  149. unsigned char reserved[128];
  150. };
  151. /* get the value type and count of the control */
  152. static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
  153. int *countp)
  154. {
  155. struct snd_kcontrol *kctl;
  156. struct snd_ctl_elem_info *info;
  157. int err;
  158. down_read(&card->controls_rwsem);
  159. kctl = snd_ctl_find_id(card, id);
  160. if (! kctl) {
  161. up_read(&card->controls_rwsem);
  162. return -ENXIO;
  163. }
  164. info = kzalloc(sizeof(*info), GFP_KERNEL);
  165. if (info == NULL) {
  166. up_read(&card->controls_rwsem);
  167. return -ENOMEM;
  168. }
  169. info->id = *id;
  170. err = kctl->info(kctl, info);
  171. up_read(&card->controls_rwsem);
  172. if (err >= 0) {
  173. err = info->type;
  174. *countp = info->count;
  175. }
  176. kfree(info);
  177. return err;
  178. }
  179. static int get_elem_size(int type, int count)
  180. {
  181. switch (type) {
  182. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  183. return sizeof(s64) * count;
  184. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  185. return sizeof(int) * count;
  186. case SNDRV_CTL_ELEM_TYPE_BYTES:
  187. return 512;
  188. case SNDRV_CTL_ELEM_TYPE_IEC958:
  189. return sizeof(struct snd_aes_iec958);
  190. default:
  191. return -1;
  192. }
  193. }
  194. static int copy_ctl_value_from_user(struct snd_card *card,
  195. struct snd_ctl_elem_value *data,
  196. struct snd_ctl_elem_value32 __user *data32,
  197. int *typep, int *countp)
  198. {
  199. int i, type, count, size;
  200. unsigned int indirect;
  201. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  202. return -EFAULT;
  203. if (get_user(indirect, &data32->indirect))
  204. return -EFAULT;
  205. if (indirect)
  206. return -EINVAL;
  207. type = get_ctl_type(card, &data->id, &count);
  208. if (type < 0)
  209. return type;
  210. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  211. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  212. for (i = 0; i < count; i++) {
  213. int val;
  214. if (get_user(val, &data32->value.integer[i]))
  215. return -EFAULT;
  216. data->value.integer.value[i] = val;
  217. }
  218. } else {
  219. size = get_elem_size(type, count);
  220. if (size < 0) {
  221. printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  222. return -EINVAL;
  223. }
  224. if (copy_from_user(data->value.bytes.data,
  225. data32->value.data, size))
  226. return -EFAULT;
  227. }
  228. *typep = type;
  229. *countp = count;
  230. return 0;
  231. }
  232. /* restore the value to 32bit */
  233. static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user *data32,
  234. struct snd_ctl_elem_value *data,
  235. int type, int count)
  236. {
  237. int i, size;
  238. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  239. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  240. for (i = 0; i < count; i++) {
  241. int val;
  242. val = data->value.integer.value[i];
  243. if (put_user(val, &data32->value.integer[i]))
  244. return -EFAULT;
  245. }
  246. } else {
  247. size = get_elem_size(type, count);
  248. if (copy_to_user(data32->value.data,
  249. data->value.bytes.data, size))
  250. return -EFAULT;
  251. }
  252. return 0;
  253. }
  254. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  255. struct snd_ctl_elem_value32 __user *data32)
  256. {
  257. struct snd_ctl_elem_value *data;
  258. int err, type, count;
  259. data = kzalloc(sizeof(*data), GFP_KERNEL);
  260. if (data == NULL)
  261. return -ENOMEM;
  262. if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
  263. goto error;
  264. if ((err = snd_ctl_elem_read(card, data)) < 0)
  265. goto error;
  266. err = copy_ctl_value_to_user(data32, data, type, count);
  267. error:
  268. kfree(data);
  269. return err;
  270. }
  271. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  272. struct snd_ctl_elem_value32 __user *data32)
  273. {
  274. struct snd_ctl_elem_value *data;
  275. int err, type, count;
  276. data = kzalloc(sizeof(*data), GFP_KERNEL);
  277. if (data == NULL)
  278. return -ENOMEM;
  279. if ((err = copy_ctl_value_from_user(file->card, data, data32, &type, &count)) < 0)
  280. goto error;
  281. if ((err = snd_ctl_elem_write(file->card, file, data)) < 0)
  282. goto error;
  283. err = copy_ctl_value_to_user(data32, data, type, count);
  284. error:
  285. kfree(data);
  286. return err;
  287. }
  288. /* add or replace a user control */
  289. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  290. struct snd_ctl_elem_info32 __user *data32,
  291. int replace)
  292. {
  293. struct snd_ctl_elem_info *data;
  294. int err;
  295. data = kzalloc(sizeof(*data), GFP_KERNEL);
  296. if (! data)
  297. return -ENOMEM;
  298. err = -EFAULT;
  299. /* id, type, access, count */ \
  300. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  301. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  302. goto error;
  303. if (get_user(data->owner, &data32->owner) ||
  304. get_user(data->type, &data32->type))
  305. goto error;
  306. switch (data->type) {
  307. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  308. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  309. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  310. get_user(data->value.integer.max, &data32->value.integer.max) ||
  311. get_user(data->value.integer.step, &data32->value.integer.step))
  312. goto error;
  313. break;
  314. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  315. if (copy_from_user(&data->value.integer64,
  316. &data32->value.integer64,
  317. sizeof(data->value.integer64)))
  318. goto error;
  319. break;
  320. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  321. if (copy_from_user(&data->value.enumerated,
  322. &data32->value.enumerated,
  323. sizeof(data->value.enumerated)))
  324. goto error;
  325. break;
  326. default:
  327. break;
  328. }
  329. err = snd_ctl_elem_add(file, data, replace);
  330. error:
  331. kfree(data);
  332. return err;
  333. }
  334. enum {
  335. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  336. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  337. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  338. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  339. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  340. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  341. };
  342. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  343. {
  344. struct snd_ctl_file *ctl;
  345. struct list_head *list;
  346. void __user *argp = compat_ptr(arg);
  347. int err;
  348. ctl = file->private_data;
  349. snd_assert(ctl && ctl->card, return -ENXIO);
  350. switch (cmd) {
  351. case SNDRV_CTL_IOCTL_PVERSION:
  352. case SNDRV_CTL_IOCTL_CARD_INFO:
  353. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  354. case SNDRV_CTL_IOCTL_POWER:
  355. case SNDRV_CTL_IOCTL_POWER_STATE:
  356. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  357. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  358. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  359. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  360. return snd_ctl_elem_list_compat(ctl->card, argp);
  361. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  362. return snd_ctl_elem_info_compat(ctl, argp);
  363. case SNDRV_CTL_IOCTL_ELEM_READ32:
  364. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  365. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  366. return snd_ctl_elem_write_user_compat(ctl, argp);
  367. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  368. return snd_ctl_elem_add_compat(ctl, argp, 0);
  369. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  370. return snd_ctl_elem_add_compat(ctl, argp, 1);
  371. }
  372. down_read(&snd_ioctl_rwsem);
  373. list_for_each(list, &snd_control_compat_ioctls) {
  374. struct snd_kctl_ioctl *p = list_entry(list, struct snd_kctl_ioctl, list);
  375. if (p->fioctl) {
  376. err = p->fioctl(ctl->card, ctl, cmd, arg);
  377. if (err != -ENOIOCTLCMD) {
  378. up_read(&snd_ioctl_rwsem);
  379. return err;
  380. }
  381. }
  382. }
  383. up_read(&snd_ioctl_rwsem);
  384. return -ENOIOCTLCMD;
  385. }