control_compat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.id = *id;
  165. err = kctl->info(kctl, &info);
  166. up_read(&card->controls_rwsem);
  167. if (err >= 0) {
  168. err = info.type;
  169. *countp = info.count;
  170. }
  171. return err;
  172. }
  173. static int get_elem_size(int type, int count)
  174. {
  175. switch (type) {
  176. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  177. return sizeof(s64) * count;
  178. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  179. return sizeof(int) * count;
  180. case SNDRV_CTL_ELEM_TYPE_BYTES:
  181. return 512;
  182. case SNDRV_CTL_ELEM_TYPE_IEC958:
  183. return sizeof(struct snd_aes_iec958);
  184. default:
  185. return -1;
  186. }
  187. }
  188. static int copy_ctl_value_from_user(struct snd_card *card,
  189. struct snd_ctl_elem_value *data,
  190. struct snd_ctl_elem_value32 __user *data32,
  191. int *typep, int *countp)
  192. {
  193. int i, type, count, size;
  194. unsigned int indirect;
  195. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  196. return -EFAULT;
  197. if (get_user(indirect, &data32->indirect))
  198. return -EFAULT;
  199. if (indirect)
  200. return -EINVAL;
  201. type = get_ctl_type(card, &data->id, &count);
  202. if (type < 0)
  203. return type;
  204. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  205. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  206. for (i = 0; i < count; i++) {
  207. int val;
  208. if (get_user(val, &data32->value.integer[i]))
  209. return -EFAULT;
  210. data->value.integer.value[i] = val;
  211. }
  212. } else {
  213. size = get_elem_size(type, count);
  214. if (size < 0) {
  215. printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  216. return -EINVAL;
  217. }
  218. if (copy_from_user(data->value.bytes.data,
  219. data32->value.data, size))
  220. return -EFAULT;
  221. }
  222. *typep = type;
  223. *countp = count;
  224. return 0;
  225. }
  226. /* restore the value to 32bit */
  227. static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user *data32,
  228. struct snd_ctl_elem_value *data,
  229. int type, int count)
  230. {
  231. int i, size;
  232. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  233. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  234. for (i = 0; i < count; i++) {
  235. int val;
  236. val = data->value.integer.value[i];
  237. if (put_user(val, &data32->value.integer[i]))
  238. return -EFAULT;
  239. }
  240. } else {
  241. size = get_elem_size(type, count);
  242. if (copy_to_user(data32->value.data,
  243. data->value.bytes.data, size))
  244. return -EFAULT;
  245. }
  246. return 0;
  247. }
  248. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  249. struct snd_ctl_elem_value32 __user *data32)
  250. {
  251. struct snd_ctl_elem_value *data;
  252. int err, type, count;
  253. data = kzalloc(sizeof(*data), GFP_KERNEL);
  254. if (data == NULL)
  255. return -ENOMEM;
  256. if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
  257. goto error;
  258. if ((err = snd_ctl_elem_read(card, data)) < 0)
  259. goto error;
  260. err = copy_ctl_value_to_user(data32, data, type, count);
  261. error:
  262. kfree(data);
  263. return err;
  264. }
  265. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  266. struct snd_ctl_elem_value32 __user *data32)
  267. {
  268. struct snd_ctl_elem_value *data;
  269. int err, type, count;
  270. data = kzalloc(sizeof(*data), GFP_KERNEL);
  271. if (data == NULL)
  272. return -ENOMEM;
  273. if ((err = copy_ctl_value_from_user(file->card, data, data32, &type, &count)) < 0)
  274. goto error;
  275. if ((err = snd_ctl_elem_write(file->card, file, data)) < 0)
  276. goto error;
  277. err = copy_ctl_value_to_user(data32, data, type, count);
  278. error:
  279. kfree(data);
  280. return err;
  281. }
  282. /* add or replace a user control */
  283. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  284. struct snd_ctl_elem_info32 __user *data32,
  285. int replace)
  286. {
  287. struct snd_ctl_elem_info *data;
  288. int err;
  289. data = kzalloc(sizeof(*data), GFP_KERNEL);
  290. if (! data)
  291. return -ENOMEM;
  292. err = -EFAULT;
  293. /* id, type, access, count */ \
  294. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  295. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  296. goto error;
  297. if (get_user(data->owner, &data32->owner) ||
  298. get_user(data->type, &data32->type))
  299. goto error;
  300. switch (data->type) {
  301. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  302. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  303. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  304. get_user(data->value.integer.max, &data32->value.integer.max) ||
  305. get_user(data->value.integer.step, &data32->value.integer.step))
  306. goto error;
  307. break;
  308. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  309. if (copy_from_user(&data->value.integer64,
  310. &data32->value.integer64,
  311. sizeof(data->value.integer64)))
  312. goto error;
  313. break;
  314. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  315. if (copy_from_user(&data->value.enumerated,
  316. &data32->value.enumerated,
  317. sizeof(data->value.enumerated)))
  318. goto error;
  319. break;
  320. default:
  321. break;
  322. }
  323. err = snd_ctl_elem_add(file, data, replace);
  324. error:
  325. kfree(data);
  326. return err;
  327. }
  328. enum {
  329. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  330. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  331. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  332. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  333. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  334. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  335. };
  336. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  337. {
  338. struct snd_ctl_file *ctl;
  339. struct list_head *list;
  340. void __user *argp = compat_ptr(arg);
  341. int err;
  342. ctl = file->private_data;
  343. snd_assert(ctl && ctl->card, return -ENXIO);
  344. switch (cmd) {
  345. case SNDRV_CTL_IOCTL_PVERSION:
  346. case SNDRV_CTL_IOCTL_CARD_INFO:
  347. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  348. case SNDRV_CTL_IOCTL_POWER:
  349. case SNDRV_CTL_IOCTL_POWER_STATE:
  350. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  351. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  352. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  353. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  354. return snd_ctl_elem_list_compat(ctl->card, argp);
  355. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  356. return snd_ctl_elem_info_compat(ctl, argp);
  357. case SNDRV_CTL_IOCTL_ELEM_READ32:
  358. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  359. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  360. return snd_ctl_elem_write_user_compat(ctl, argp);
  361. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  362. return snd_ctl_elem_add_compat(ctl, argp, 0);
  363. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  364. return snd_ctl_elem_add_compat(ctl, argp, 1);
  365. }
  366. down_read(&snd_ioctl_rwsem);
  367. list_for_each(list, &snd_control_compat_ioctls) {
  368. struct snd_kctl_ioctl *p = list_entry(list, struct snd_kctl_ioctl, list);
  369. if (p->fioctl) {
  370. err = p->fioctl(ctl->card, ctl, cmd, arg);
  371. if (err != -ENOIOCTLCMD) {
  372. up_read(&snd_ioctl_rwsem);
  373. return err;
  374. }
  375. }
  376. }
  377. up_read(&snd_ioctl_rwsem);
  378. return -ENOIOCTLCMD;
  379. }