control_compat.c 11 KB

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