Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Copyright 2014 Red Hat Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 *
 22 * Authors: Ben Skeggs <bskeggs@redhat.com>
 23 */
 24
 25#include <nvif/object.h>
 26#include <nvif/client.h>
 27#include <nvif/driver.h>
 28#include <nvif/ioctl.h>
 29
 30int
 31nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
 32{
 33	struct nvif_client *client = object->client;
 34	union {
 35		struct nvif_ioctl_v0 v0;
 36	} *args = data;
 37
 38	if (size >= sizeof(*args) && args->v0.version == 0) {
 39		if (object != &client->object)
 40			args->v0.object = nvif_handle(object);
 41		else
 42			args->v0.object = 0;
 43		args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
 44	} else
 45		return -ENOSYS;
 46
 47	return client->driver->ioctl(client->object.priv, client->super,
 48				     data, size, hack);
 49}
 50
 51void
 52nvif_object_sclass_put(struct nvif_sclass **psclass)
 53{
 54	kfree(*psclass);
 55	*psclass = NULL;
 56}
 57
 58int
 59nvif_object_sclass_get(struct nvif_object *object, struct nvif_sclass **psclass)
 60{
 61	struct {
 62		struct nvif_ioctl_v0 ioctl;
 63		struct nvif_ioctl_sclass_v0 sclass;
 64	} *args = NULL;
 65	int ret, cnt = 0, i;
 66	u32 size;
 67
 68	while (1) {
 69		size = sizeof(*args) + cnt * sizeof(args->sclass.oclass[0]);
 70		if (!(args = kmalloc(size, GFP_KERNEL)))
 71			return -ENOMEM;
 72		args->ioctl.version = 0;
 73		args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
 74		args->sclass.version = 0;
 75		args->sclass.count = cnt;
 76
 77		ret = nvif_object_ioctl(object, args, size, NULL);
 78		if (ret == 0 && args->sclass.count <= cnt)
 79			break;
 80		cnt = args->sclass.count;
 81		kfree(args);
 82		if (ret != 0)
 83			return ret;
 84	}
 85
 86	*psclass = kzalloc(sizeof(**psclass) * args->sclass.count, GFP_KERNEL);
 87	if (*psclass) {
 88		for (i = 0; i < args->sclass.count; i++) {
 89			(*psclass)[i].oclass = args->sclass.oclass[i].oclass;
 90			(*psclass)[i].minver = args->sclass.oclass[i].minver;
 91			(*psclass)[i].maxver = args->sclass.oclass[i].maxver;
 92		}
 93		ret = args->sclass.count;
 94	} else {
 95		ret = -ENOMEM;
 96	}
 97
 98	kfree(args);
 99	return ret;
100}
101
102u32
103nvif_object_rd(struct nvif_object *object, int size, u64 addr)
104{
105	struct {
106		struct nvif_ioctl_v0 ioctl;
107		struct nvif_ioctl_rd_v0 rd;
108	} args = {
109		.ioctl.type = NVIF_IOCTL_V0_RD,
110		.rd.size = size,
111		.rd.addr = addr,
112	};
113	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
114	if (ret) {
115		/*XXX: warn? */
116		return 0;
117	}
118	return args.rd.data;
119}
120
121void
122nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data)
123{
124	struct {
125		struct nvif_ioctl_v0 ioctl;
126		struct nvif_ioctl_wr_v0 wr;
127	} args = {
128		.ioctl.type = NVIF_IOCTL_V0_WR,
129		.wr.size = size,
130		.wr.addr = addr,
131		.wr.data = data,
132	};
133	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
134	if (ret) {
135		/*XXX: warn? */
136	}
137}
138
139int
140nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
141{
142	struct {
143		struct nvif_ioctl_v0 ioctl;
144		struct nvif_ioctl_mthd_v0 mthd;
145	} *args;
 
146	u8 stack[128];
147	int ret;
148
149	if (sizeof(*args) + size > sizeof(stack)) {
150		if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
 
 
 
 
151			return -ENOMEM;
152	} else {
153		args = (void *)stack;
154	}
155	args->ioctl.version = 0;
156	args->ioctl.type = NVIF_IOCTL_V0_MTHD;
157	args->mthd.version = 0;
158	args->mthd.method = mthd;
159
160	memcpy(args->mthd.data, data, size);
161	ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
162	memcpy(data, args->mthd.data, size);
163	if (args != (void *)stack)
164		kfree(args);
165	return ret;
166}
167
168void
169nvif_object_unmap(struct nvif_object *object)
170{
171	if (object->map.size) {
172		struct nvif_client *client = object->client;
173		struct {
174			struct nvif_ioctl_v0 ioctl;
175			struct nvif_ioctl_unmap unmap;
176		} args = {
177			.ioctl.type = NVIF_IOCTL_V0_UNMAP,
178		};
 
179
180		if (object->map.ptr) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181			client->driver->unmap(client, object->map.ptr,
182						      object->map.size);
183			object->map.ptr = NULL;
184		}
185
186		nvif_object_ioctl(object, &args, sizeof(args), NULL);
187		object->map.size = 0;
188	}
189}
190
191int
192nvif_object_map(struct nvif_object *object)
193{
194	struct nvif_client *client = object->client;
195	struct {
196		struct nvif_ioctl_v0 ioctl;
197		struct nvif_ioctl_map_v0 map;
198	} args = {
199		.ioctl.type = NVIF_IOCTL_V0_MAP,
200	};
201	int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
202	if (ret == 0) {
203		object->map.size = args.map.length;
204		object->map.ptr = client->driver->map(client, args.map.handle,
205						      object->map.size);
206		if (ret = -ENOMEM, object->map.ptr)
 
207			return 0;
208		nvif_object_unmap(object);
 
209	}
210	return ret;
211}
212
213void
214nvif_object_fini(struct nvif_object *object)
215{
216	struct {
217		struct nvif_ioctl_v0 ioctl;
218		struct nvif_ioctl_del del;
219	} args = {
220		.ioctl.type = NVIF_IOCTL_V0_DEL,
221	};
222
223	if (!object->client)
224		return;
225
226	nvif_object_unmap(object);
227	nvif_object_ioctl(object, &args, sizeof(args), NULL);
228	object->client = NULL;
229}
230
231int
232nvif_object_init(struct nvif_object *parent, u32 handle, s32 oclass,
233		 void *data, u32 size, struct nvif_object *object)
234{
235	struct {
236		struct nvif_ioctl_v0 ioctl;
237		struct nvif_ioctl_new_v0 new;
238	} *args;
239	int ret = 0;
240
241	object->client = NULL;
 
242	object->handle = handle;
243	object->oclass = oclass;
244	object->map.ptr = NULL;
245	object->map.size = 0;
246
247	if (parent) {
248		if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
249			nvif_object_fini(object);
 
 
 
 
 
 
 
 
250			return -ENOMEM;
251		}
252
 
 
253		args->ioctl.version = 0;
254		args->ioctl.type = NVIF_IOCTL_V0_NEW;
255		args->new.version = 0;
256		args->new.route = parent->client->route;
257		args->new.token = nvif_handle(object);
258		args->new.object = nvif_handle(object);
259		args->new.handle = handle;
260		args->new.oclass = oclass;
261
262		memcpy(args->new.data, data, size);
263		ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
264					&object->priv);
265		memcpy(data, args->new.data, size);
266		kfree(args);
267		if (ret == 0)
268			object->client = parent->client;
269	}
270
271	if (ret)
272		nvif_object_fini(object);
273	return ret;
274}
v6.13.7
  1/*
  2 * Copyright 2014 Red Hat Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 *
 22 * Authors: Ben Skeggs <bskeggs@redhat.com>
 23 */
 24
 25#include <nvif/object.h>
 26#include <nvif/client.h>
 27#include <nvif/driver.h>
 28#include <nvif/ioctl.h>
 29
 30int
 31nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
 32{
 33	struct nvif_client *client = object->client;
 34	union {
 35		struct nvif_ioctl_v0 v0;
 36	} *args = data;
 37
 38	if (size >= sizeof(*args) && args->v0.version == 0) {
 39		if (object != &client->object)
 40			args->v0.object = nvif_handle(object);
 41		else
 42			args->v0.object = 0;
 
 43	} else
 44		return -ENOSYS;
 45
 46	return client->driver->ioctl(client->object.priv, data, size, hack);
 
 47}
 48
 49void
 50nvif_object_sclass_put(struct nvif_sclass **psclass)
 51{
 52	kfree(*psclass);
 53	*psclass = NULL;
 54}
 55
 56int
 57nvif_object_sclass_get(struct nvif_object *object, struct nvif_sclass **psclass)
 58{
 59	struct {
 60		struct nvif_ioctl_v0 ioctl;
 61		struct nvif_ioctl_sclass_v0 sclass;
 62	} *args = NULL;
 63	int ret, cnt = 0, i;
 64	u32 size;
 65
 66	while (1) {
 67		size = sizeof(*args) + cnt * sizeof(args->sclass.oclass[0]);
 68		if (!(args = kmalloc(size, GFP_KERNEL)))
 69			return -ENOMEM;
 70		args->ioctl.version = 0;
 71		args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
 72		args->sclass.version = 0;
 73		args->sclass.count = cnt;
 74
 75		ret = nvif_object_ioctl(object, args, size, NULL);
 76		if (ret == 0 && args->sclass.count <= cnt)
 77			break;
 78		cnt = args->sclass.count;
 79		kfree(args);
 80		if (ret != 0)
 81			return ret;
 82	}
 83
 84	*psclass = kcalloc(args->sclass.count, sizeof(**psclass), GFP_KERNEL);
 85	if (*psclass) {
 86		for (i = 0; i < args->sclass.count; i++) {
 87			(*psclass)[i].oclass = args->sclass.oclass[i].oclass;
 88			(*psclass)[i].minver = args->sclass.oclass[i].minver;
 89			(*psclass)[i].maxver = args->sclass.oclass[i].maxver;
 90		}
 91		ret = args->sclass.count;
 92	} else {
 93		ret = -ENOMEM;
 94	}
 95
 96	kfree(args);
 97	return ret;
 98}
 99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100int
101nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
102{
103	struct {
104		struct nvif_ioctl_v0 ioctl;
105		struct nvif_ioctl_mthd_v0 mthd;
106	} *args;
107	u32 args_size;
108	u8 stack[128];
109	int ret;
110
111	if (check_add_overflow(sizeof(*args), size, &args_size))
112		return -ENOMEM;
113
114	if (args_size > sizeof(stack)) {
115		args = kmalloc(args_size, GFP_KERNEL);
116		if (!args)
117			return -ENOMEM;
118	} else {
119		args = (void *)stack;
120	}
121	args->ioctl.version = 0;
122	args->ioctl.type = NVIF_IOCTL_V0_MTHD;
123	args->mthd.version = 0;
124	args->mthd.method = mthd;
125
126	memcpy(args->mthd.data, data, size);
127	ret = nvif_object_ioctl(object, args, args_size, NULL);
128	memcpy(data, args->mthd.data, size);
129	if (args != (void *)stack)
130		kfree(args);
131	return ret;
132}
133
134void
135nvif_object_unmap_handle(struct nvif_object *object)
136{
137	struct {
138		struct nvif_ioctl_v0 ioctl;
139		struct nvif_ioctl_unmap unmap;
140	} args = {
141		.ioctl.type = NVIF_IOCTL_V0_UNMAP,
142	};
143
144	nvif_object_ioctl(object, &args, sizeof(args), NULL);
145}
146
147int
148nvif_object_map_handle(struct nvif_object *object, void *argv, u32 argc,
149		       u64 *handle, u64 *length)
150{
151	struct {
152		struct nvif_ioctl_v0 ioctl;
153		struct nvif_ioctl_map_v0 map;
154	} *args;
155	u32 argn = sizeof(*args) + argc;
156	int ret, maptype;
157
158	if (!(args = kzalloc(argn, GFP_KERNEL)))
159		return -ENOMEM;
160	args->ioctl.type = NVIF_IOCTL_V0_MAP;
161	memcpy(args->map.data, argv, argc);
162
163	ret = nvif_object_ioctl(object, args, argn, NULL);
164	*handle = args->map.handle;
165	*length = args->map.length;
166	maptype = args->map.type;
167	kfree(args);
168	return ret ? ret : (maptype == NVIF_IOCTL_MAP_V0_IO);
169}
170
171void
172nvif_object_unmap(struct nvif_object *object)
173{
174	struct nvif_client *client = object->client;
175	if (object->map.ptr) {
176		if (object->map.size) {
177			client->driver->unmap(client, object->map.ptr,
178						      object->map.size);
179			object->map.size = 0;
180		}
181		object->map.ptr = NULL;
182		nvif_object_unmap_handle(object);
 
183	}
184}
185
186int
187nvif_object_map(struct nvif_object *object, void *argv, u32 argc)
188{
189	struct nvif_client *client = object->client;
190	u64 handle, length;
191	int ret = nvif_object_map_handle(object, argv, argc, &handle, &length);
192	if (ret >= 0) {
193		if (ret) {
194			object->map.ptr = client->driver->map(client,
195							      handle,
196							      length);
197			if (ret = -ENOMEM, object->map.ptr) {
198				object->map.size = length;
199				return 0;
200			}
201		} else {
202			object->map.ptr = (void *)(unsigned long)handle;
203			return 0;
204		}
205		nvif_object_unmap_handle(object);
206	}
207	return ret;
208}
209
210void
211nvif_object_dtor(struct nvif_object *object)
212{
213	struct {
214		struct nvif_ioctl_v0 ioctl;
215		struct nvif_ioctl_del del;
216	} args = {
217		.ioctl.type = NVIF_IOCTL_V0_DEL,
218	};
219
220	if (!nvif_object_constructed(object))
221		return;
222
223	nvif_object_unmap(object);
224	nvif_object_ioctl(object, &args, sizeof(args), NULL);
225	object->client = NULL;
226}
227
228int
229nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle,
230		 s32 oclass, void *data, u32 size, struct nvif_object *object)
231{
232	struct {
233		struct nvif_ioctl_v0 ioctl;
234		struct nvif_ioctl_new_v0 new;
235	} *args;
236	int ret = 0;
237
238	object->client = NULL;
239	object->name = name ? name : "nvifObject";
240	object->handle = handle;
241	object->oclass = oclass;
242	object->map.ptr = NULL;
243	object->map.size = 0;
244
245	if (parent) {
246		u32 args_size;
247
248		if (check_add_overflow(sizeof(*args), size, &args_size)) {
249			nvif_object_dtor(object);
250			return -ENOMEM;
251		}
252
253		args = kmalloc(args_size, GFP_KERNEL);
254		if (!args) {
255			nvif_object_dtor(object);
256			return -ENOMEM;
257		}
258
259		object->parent = parent->parent;
260
261		args->ioctl.version = 0;
262		args->ioctl.type = NVIF_IOCTL_V0_NEW;
263		args->new.version = 0;
 
 
264		args->new.object = nvif_handle(object);
265		args->new.handle = handle;
266		args->new.oclass = oclass;
267
268		memcpy(args->new.data, data, size);
269		ret = nvif_object_ioctl(parent, args, args_size, &object->priv);
 
270		memcpy(data, args->new.data, size);
271		kfree(args);
272		if (ret == 0)
273			object->client = parent->client;
274	}
275
276	if (ret)
277		nvif_object_dtor(object);
278	return ret;
279}