control_compat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  103. if (err < 0)
  104. goto error;
  105. err = snd_ctl_elem_info(ctl, data);
  106. if (err < 0)
  107. goto error;
  108. /* restore info to 32bit */
  109. err = -EFAULT;
  110. /* id, type, access, count */
  111. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  112. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  113. goto error;
  114. if (put_user(data->owner, &data32->owner))
  115. goto error;
  116. switch (data->type) {
  117. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  118. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  119. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  120. put_user(data->value.integer.max, &data32->value.integer.max) ||
  121. put_user(data->value.integer.step, &data32->value.integer.step))
  122. goto error;
  123. break;
  124. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  125. if (copy_to_user(&data32->value.integer64,
  126. &data->value.integer64,
  127. sizeof(data->value.integer64)))
  128. goto error;
  129. break;
  130. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  131. if (copy_to_user(&data32->value.enumerated,
  132. &data->value.enumerated,
  133. sizeof(data->value.enumerated)))
  134. goto error;
  135. break;
  136. default:
  137. break;
  138. }
  139. err = 0;
  140. error:
  141. kfree(data);
  142. return err;
  143. }
  144. /* read / write */
  145. struct snd_ctl_elem_value32 {
  146. struct snd_ctl_elem_id id;
  147. unsigned int indirect; /* bit-field causes misalignment */
  148. union {
  149. s32 integer[128];
  150. unsigned char data[512];
  151. #ifndef CONFIG_X86_64
  152. s64 integer64[64];
  153. #endif
  154. } value;
  155. unsigned char reserved[128];
  156. };
  157. #ifdef CONFIG_X86_X32
  158. /* x32 has a different alignment for 64bit values from ia32 */
  159. struct snd_ctl_elem_value_x32 {
  160. struct snd_ctl_elem_id id;
  161. unsigned int indirect; /* bit-field causes misalignment */
  162. union {
  163. s32 integer[128];
  164. unsigned char data[512];
  165. s64 integer64[64];
  166. } value;
  167. unsigned char reserved[128];
  168. };
  169. #endif /* CONFIG_X86_X32 */
  170. /* get the value type and count of the control */
  171. static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
  172. int *countp)
  173. {
  174. struct snd_kcontrol *kctl;
  175. struct snd_ctl_elem_info *info;
  176. int err;
  177. down_read(&card->controls_rwsem);
  178. kctl = snd_ctl_find_id(card, id);
  179. if (! kctl) {
  180. up_read(&card->controls_rwsem);
  181. return -ENOENT;
  182. }
  183. info = kzalloc(sizeof(*info), GFP_KERNEL);
  184. if (info == NULL) {
  185. up_read(&card->controls_rwsem);
  186. return -ENOMEM;
  187. }
  188. info->id = *id;
  189. err = kctl->info(kctl, info);
  190. up_read(&card->controls_rwsem);
  191. if (err >= 0) {
  192. err = info->type;
  193. *countp = info->count;
  194. }
  195. kfree(info);
  196. return err;
  197. }
  198. static int get_elem_size(int type, int count)
  199. {
  200. switch (type) {
  201. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  202. return sizeof(s64) * count;
  203. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  204. return sizeof(int) * count;
  205. case SNDRV_CTL_ELEM_TYPE_BYTES:
  206. return 512;
  207. case SNDRV_CTL_ELEM_TYPE_IEC958:
  208. return sizeof(struct snd_aes_iec958);
  209. default:
  210. return -1;
  211. }
  212. }
  213. static int copy_ctl_value_from_user(struct snd_card *card,
  214. struct snd_ctl_elem_value *data,
  215. void __user *userdata,
  216. void __user *valuep,
  217. int *typep, int *countp)
  218. {
  219. struct snd_ctl_elem_value32 __user *data32 = userdata;
  220. int i, type, size;
  221. int uninitialized_var(count);
  222. unsigned int indirect;
  223. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  224. return -EFAULT;
  225. if (get_user(indirect, &data32->indirect))
  226. return -EFAULT;
  227. if (indirect)
  228. return -EINVAL;
  229. type = get_ctl_type(card, &data->id, &count);
  230. if (type < 0)
  231. return type;
  232. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  233. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  234. for (i = 0; i < count; i++) {
  235. s32 __user *intp = valuep;
  236. int val;
  237. if (get_user(val, &intp[i]))
  238. return -EFAULT;
  239. data->value.integer.value[i] = val;
  240. }
  241. } else {
  242. size = get_elem_size(type, count);
  243. if (size < 0) {
  244. dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  245. return -EINVAL;
  246. }
  247. if (copy_from_user(data->value.bytes.data, valuep, size))
  248. return -EFAULT;
  249. }
  250. *typep = type;
  251. *countp = count;
  252. return 0;
  253. }
  254. /* restore the value to 32bit */
  255. static int copy_ctl_value_to_user(void __user *userdata,
  256. void __user *valuep,
  257. struct snd_ctl_elem_value *data,
  258. int type, int count)
  259. {
  260. int i, size;
  261. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  262. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  263. for (i = 0; i < count; i++) {
  264. s32 __user *intp = valuep;
  265. int val;
  266. val = data->value.integer.value[i];
  267. if (put_user(val, &intp[i]))
  268. return -EFAULT;
  269. }
  270. } else {
  271. size = get_elem_size(type, count);
  272. if (copy_to_user(valuep, data->value.bytes.data, size))
  273. return -EFAULT;
  274. }
  275. return 0;
  276. }
  277. static int ctl_elem_read_user(struct snd_card *card,
  278. void __user *userdata, void __user *valuep)
  279. {
  280. struct snd_ctl_elem_value *data;
  281. int err, type, count;
  282. data = kzalloc(sizeof(*data), GFP_KERNEL);
  283. if (data == NULL)
  284. return -ENOMEM;
  285. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  286. &type, &count);
  287. if (err < 0)
  288. goto error;
  289. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  290. if (err < 0)
  291. goto error;
  292. err = snd_ctl_elem_read(card, data);
  293. if (err < 0)
  294. goto error;
  295. err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
  296. error:
  297. kfree(data);
  298. return err;
  299. }
  300. static int ctl_elem_write_user(struct snd_ctl_file *file,
  301. void __user *userdata, void __user *valuep)
  302. {
  303. struct snd_ctl_elem_value *data;
  304. struct snd_card *card = file->card;
  305. int err, type, count;
  306. data = kzalloc(sizeof(*data), GFP_KERNEL);
  307. if (data == NULL)
  308. return -ENOMEM;
  309. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  310. &type, &count);
  311. if (err < 0)
  312. goto error;
  313. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  314. if (err < 0)
  315. goto error;
  316. err = snd_ctl_elem_write(card, file, data);
  317. if (err < 0)
  318. goto error;
  319. err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
  320. error:
  321. kfree(data);
  322. return err;
  323. }
  324. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  325. struct snd_ctl_elem_value32 __user *data32)
  326. {
  327. return ctl_elem_read_user(card, data32, &data32->value);
  328. }
  329. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  330. struct snd_ctl_elem_value32 __user *data32)
  331. {
  332. return ctl_elem_write_user(file, data32, &data32->value);
  333. }
  334. #ifdef CONFIG_X86_X32
  335. static int snd_ctl_elem_read_user_x32(struct snd_card *card,
  336. struct snd_ctl_elem_value_x32 __user *data32)
  337. {
  338. return ctl_elem_read_user(card, data32, &data32->value);
  339. }
  340. static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
  341. struct snd_ctl_elem_value_x32 __user *data32)
  342. {
  343. return ctl_elem_write_user(file, data32, &data32->value);
  344. }
  345. #endif /* CONFIG_X86_X32 */
  346. /* add or replace a user control */
  347. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  348. struct snd_ctl_elem_info32 __user *data32,
  349. int replace)
  350. {
  351. struct snd_ctl_elem_info *data;
  352. int err;
  353. data = kzalloc(sizeof(*data), GFP_KERNEL);
  354. if (! data)
  355. return -ENOMEM;
  356. err = -EFAULT;
  357. /* id, type, access, count */ \
  358. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  359. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  360. goto error;
  361. if (get_user(data->owner, &data32->owner))
  362. goto error;
  363. switch (data->type) {
  364. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  365. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  366. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  367. get_user(data->value.integer.max, &data32->value.integer.max) ||
  368. get_user(data->value.integer.step, &data32->value.integer.step))
  369. goto error;
  370. break;
  371. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  372. if (copy_from_user(&data->value.integer64,
  373. &data32->value.integer64,
  374. sizeof(data->value.integer64)))
  375. goto error;
  376. break;
  377. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  378. if (copy_from_user(&data->value.enumerated,
  379. &data32->value.enumerated,
  380. sizeof(data->value.enumerated)))
  381. goto error;
  382. data->value.enumerated.names_ptr =
  383. (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
  384. break;
  385. default:
  386. break;
  387. }
  388. err = snd_ctl_elem_add(file, data, replace);
  389. error:
  390. kfree(data);
  391. return err;
  392. }
  393. enum {
  394. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  395. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  396. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  397. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  398. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  399. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  400. #ifdef CONFIG_X86_X32
  401. SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
  402. SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
  403. #endif /* CONFIG_X86_X32 */
  404. };
  405. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  406. {
  407. struct snd_ctl_file *ctl;
  408. struct snd_kctl_ioctl *p;
  409. void __user *argp = compat_ptr(arg);
  410. int err;
  411. ctl = file->private_data;
  412. if (snd_BUG_ON(!ctl || !ctl->card))
  413. return -ENXIO;
  414. switch (cmd) {
  415. case SNDRV_CTL_IOCTL_PVERSION:
  416. case SNDRV_CTL_IOCTL_CARD_INFO:
  417. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  418. case SNDRV_CTL_IOCTL_POWER:
  419. case SNDRV_CTL_IOCTL_POWER_STATE:
  420. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  421. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  422. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  423. case SNDRV_CTL_IOCTL_TLV_READ:
  424. case SNDRV_CTL_IOCTL_TLV_WRITE:
  425. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  426. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  427. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  428. return snd_ctl_elem_list_compat(ctl->card, argp);
  429. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  430. return snd_ctl_elem_info_compat(ctl, argp);
  431. case SNDRV_CTL_IOCTL_ELEM_READ32:
  432. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  433. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  434. return snd_ctl_elem_write_user_compat(ctl, argp);
  435. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  436. return snd_ctl_elem_add_compat(ctl, argp, 0);
  437. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  438. return snd_ctl_elem_add_compat(ctl, argp, 1);
  439. #ifdef CONFIG_X86_X32
  440. case SNDRV_CTL_IOCTL_ELEM_READ_X32:
  441. return snd_ctl_elem_read_user_x32(ctl->card, argp);
  442. case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
  443. return snd_ctl_elem_write_user_x32(ctl, argp);
  444. #endif /* CONFIG_X86_X32 */
  445. }
  446. down_read(&snd_ioctl_rwsem);
  447. list_for_each_entry(p, &snd_control_compat_ioctls, list) {
  448. if (p->fioctl) {
  449. err = p->fioctl(ctl->card, ctl, cmd, arg);
  450. if (err != -ENOIOCTLCMD) {
  451. up_read(&snd_ioctl_rwsem);
  452. return err;
  453. }
  454. }
  455. }
  456. up_read(&snd_ioctl_rwsem);
  457. return -ENOIOCTLCMD;
  458. }