Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Copyright © 2015 Intel Corporation
  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 (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21 * IN THE SOFTWARE.
 22 *
 23 * Authors:
 24 *    Rafael Antognolli <rafael.antognolli@intel.com>
 25 *
 26 */
 27
 28#include <linux/device.h>
 29#include <linux/fs.h>
 30#include <linux/slab.h>
 31#include <linux/init.h>
 32#include <linux/kernel.h>
 33#include <linux/module.h>
 
 
 34#include <linux/uaccess.h>
 35#include <drm/drm_dp_helper.h>
 
 36#include <drm/drm_crtc.h>
 37#include <drm/drmP.h>
 
 
 
 
 38
 39struct drm_dp_aux_dev {
 40	unsigned index;
 41	struct drm_dp_aux *aux;
 42	struct device *dev;
 43	struct kref refcount;
 44	atomic_t usecount;
 45};
 46
 47#define DRM_AUX_MINORS	256
 48#define AUX_MAX_OFFSET	(1 << 20)
 49static DEFINE_IDR(aux_idr);
 50static DEFINE_MUTEX(aux_idr_mutex);
 51static struct class *drm_dp_aux_dev_class;
 52static int drm_dev_major = -1;
 53
 54static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
 55{
 56	struct drm_dp_aux_dev *aux_dev = NULL;
 57
 58	mutex_lock(&aux_idr_mutex);
 59	aux_dev = idr_find(&aux_idr, index);
 60	if (!kref_get_unless_zero(&aux_dev->refcount))
 61		aux_dev = NULL;
 62	mutex_unlock(&aux_idr_mutex);
 63
 64	return aux_dev;
 65}
 66
 67static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
 68{
 69	struct drm_dp_aux_dev *aux_dev;
 70	int index;
 71
 72	aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
 73	if (!aux_dev)
 74		return ERR_PTR(-ENOMEM);
 75	aux_dev->aux = aux;
 76	atomic_set(&aux_dev->usecount, 1);
 77	kref_init(&aux_dev->refcount);
 78
 79	mutex_lock(&aux_idr_mutex);
 80	index = idr_alloc_cyclic(&aux_idr, aux_dev, 0, DRM_AUX_MINORS,
 81				 GFP_KERNEL);
 82	mutex_unlock(&aux_idr_mutex);
 83	if (index < 0) {
 84		kfree(aux_dev);
 85		return ERR_PTR(index);
 86	}
 87	aux_dev->index = index;
 88
 89	return aux_dev;
 90}
 91
 92static void release_drm_dp_aux_dev(struct kref *ref)
 93{
 94	struct drm_dp_aux_dev *aux_dev =
 95		container_of(ref, struct drm_dp_aux_dev, refcount);
 96
 97	kfree(aux_dev);
 98}
 99
100static ssize_t name_show(struct device *dev,
101			 struct device_attribute *attr, char *buf)
102{
103	ssize_t res;
104	struct drm_dp_aux_dev *aux_dev =
105		drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
106
107	if (!aux_dev)
108		return -ENODEV;
109
110	res = sprintf(buf, "%s\n", aux_dev->aux->name);
111	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
112
113	return res;
114}
115static DEVICE_ATTR_RO(name);
116
117static struct attribute *drm_dp_aux_attrs[] = {
118	&dev_attr_name.attr,
119	NULL,
120};
121ATTRIBUTE_GROUPS(drm_dp_aux);
122
123static int auxdev_open(struct inode *inode, struct file *file)
124{
125	unsigned int minor = iminor(inode);
126	struct drm_dp_aux_dev *aux_dev;
127
128	aux_dev = drm_dp_aux_dev_get_by_minor(minor);
129	if (!aux_dev)
130		return -ENODEV;
131
132	file->private_data = aux_dev;
133	return 0;
134}
135
136static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
137{
138	return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
139}
140
141static ssize_t auxdev_read(struct file *file, char __user *buf, size_t count,
142			   loff_t *offset)
143{
144	size_t bytes_pending, num_bytes_processed = 0;
145	struct drm_dp_aux_dev *aux_dev = file->private_data;
146	ssize_t res = 0;
147
148	if (!atomic_inc_not_zero(&aux_dev->usecount))
149		return -ENODEV;
150
151	bytes_pending = min((loff_t)count, AUX_MAX_OFFSET - (*offset));
152
153	if (!access_ok(VERIFY_WRITE, buf, bytes_pending)) {
154		res = -EFAULT;
155		goto out;
156	}
157
158	while (bytes_pending > 0) {
159		uint8_t localbuf[DP_AUX_MAX_PAYLOAD_BYTES];
160		ssize_t todo = min_t(size_t, bytes_pending, sizeof(localbuf));
161
162		res = drm_dp_dpcd_read(aux_dev->aux, *offset, localbuf, todo);
163		if (res <= 0) {
164			res = num_bytes_processed ? num_bytes_processed : res;
165			goto out;
166		}
167		if (__copy_to_user(buf + num_bytes_processed, localbuf, res)) {
168			res = num_bytes_processed ?
169				num_bytes_processed : -EFAULT;
170			goto out;
 
 
 
 
 
 
 
 
 
171		}
172		bytes_pending -= res;
173		*offset += res;
174		num_bytes_processed += res;
175		res = num_bytes_processed;
176	}
177
178out:
179	atomic_dec(&aux_dev->usecount);
180	wake_up_atomic_t(&aux_dev->usecount);
 
 
 
 
181	return res;
182}
183
184static ssize_t auxdev_write(struct file *file, const char __user *buf,
185			    size_t count, loff_t *offset)
186{
187	size_t bytes_pending, num_bytes_processed = 0;
188	struct drm_dp_aux_dev *aux_dev = file->private_data;
189	ssize_t res = 0;
190
191	if (!atomic_inc_not_zero(&aux_dev->usecount))
192		return -ENODEV;
193
194	bytes_pending = min((loff_t)count, AUX_MAX_OFFSET - *offset);
195
196	if (!access_ok(VERIFY_READ, buf, bytes_pending)) {
197		res = -EFAULT;
198		goto out;
199	}
200
201	while (bytes_pending > 0) {
202		uint8_t localbuf[DP_AUX_MAX_PAYLOAD_BYTES];
203		ssize_t todo = min_t(size_t, bytes_pending, sizeof(localbuf));
204
205		if (__copy_from_user(localbuf,
206				     buf + num_bytes_processed, todo)) {
207			res = num_bytes_processed ?
208				num_bytes_processed : -EFAULT;
209			goto out;
210		}
211
212		res = drm_dp_dpcd_write(aux_dev->aux, *offset, localbuf, todo);
213		if (res <= 0) {
214			res = num_bytes_processed ? num_bytes_processed : res;
215			goto out;
216		}
217		bytes_pending -= res;
218		*offset += res;
219		num_bytes_processed += res;
220		res = num_bytes_processed;
 
 
 
 
 
 
 
221	}
222
223out:
224	atomic_dec(&aux_dev->usecount);
225	wake_up_atomic_t(&aux_dev->usecount);
 
 
 
 
226	return res;
227}
228
229static int auxdev_release(struct inode *inode, struct file *file)
230{
231	struct drm_dp_aux_dev *aux_dev = file->private_data;
232
233	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
234	return 0;
235}
236
237static const struct file_operations auxdev_fops = {
238	.owner		= THIS_MODULE,
239	.llseek		= auxdev_llseek,
240	.read		= auxdev_read,
241	.write		= auxdev_write,
242	.open		= auxdev_open,
243	.release	= auxdev_release,
244};
245
246#define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
247
248static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
249{
250	struct drm_dp_aux_dev *iter, *aux_dev = NULL;
251	int id;
252
253	/* don't increase kref count here because this function should only be
254	 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
255	 * least one reference - the one that drm_dp_aux_register_devnode
256	 * created
257	 */
258	mutex_lock(&aux_idr_mutex);
259	idr_for_each_entry(&aux_idr, iter, id) {
260		if (iter->aux == aux) {
261			aux_dev = iter;
262			break;
263		}
264	}
265	mutex_unlock(&aux_idr_mutex);
266	return aux_dev;
267}
268
269static int auxdev_wait_atomic_t(atomic_t *p)
270{
271	schedule();
272	return 0;
273}
274/**
275 * drm_dp_aux_unregister_devnode() - unregister a devnode for this aux channel
276 * @aux: DisplayPort AUX channel
277 *
278 * Returns 0 on success or a negative error code on failure.
279 */
280void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
281{
282	struct drm_dp_aux_dev *aux_dev;
283	unsigned int minor;
284
285	aux_dev = drm_dp_aux_dev_get_by_aux(aux);
286	if (!aux_dev) /* attach must have failed */
287		return;
288
289	mutex_lock(&aux_idr_mutex);
290	idr_remove(&aux_idr, aux_dev->index);
291	mutex_unlock(&aux_idr_mutex);
292
293	atomic_dec(&aux_dev->usecount);
294	wait_on_atomic_t(&aux_dev->usecount, auxdev_wait_atomic_t,
295			 TASK_UNINTERRUPTIBLE);
296
297	minor = aux_dev->index;
298	if (aux_dev->dev)
299		device_destroy(drm_dp_aux_dev_class,
300			       MKDEV(drm_dev_major, minor));
301
302	DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
303	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
304}
305EXPORT_SYMBOL(drm_dp_aux_unregister_devnode);
306
307/**
308 * drm_dp_aux_register_devnode() - register a devnode for this aux channel
309 * @aux: DisplayPort AUX channel
310 *
311 * Returns 0 on success or a negative error code on failure.
312 */
313int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
314{
315	struct drm_dp_aux_dev *aux_dev;
316	int res;
317
318	aux_dev = alloc_drm_dp_aux_dev(aux);
319	if (IS_ERR(aux_dev))
320		return PTR_ERR(aux_dev);
321
322	aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
323				     MKDEV(drm_dev_major, aux_dev->index), NULL,
324				     "drm_dp_aux%d", aux_dev->index);
325	if (IS_ERR(aux_dev->dev)) {
326		res = PTR_ERR(aux_dev->dev);
327		aux_dev->dev = NULL;
328		goto error;
329	}
330
331	DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
332		  aux->name, aux_dev->index);
333	return 0;
334error:
335	drm_dp_aux_unregister_devnode(aux);
336	return res;
337}
338EXPORT_SYMBOL(drm_dp_aux_register_devnode);
339
340int drm_dp_aux_dev_init(void)
341{
342	int res;
343
344	drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
345	if (IS_ERR(drm_dp_aux_dev_class)) {
346		res = PTR_ERR(drm_dp_aux_dev_class);
347		goto out;
348	}
349	drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
350
351	res = register_chrdev(0, "aux", &auxdev_fops);
352	if (res < 0)
353		goto out;
354	drm_dev_major = res;
355
356	return 0;
357out:
358	class_destroy(drm_dp_aux_dev_class);
359	return res;
360}
361EXPORT_SYMBOL(drm_dp_aux_dev_init);
362
363void drm_dp_aux_dev_exit(void)
364{
365	unregister_chrdev(drm_dev_major, "aux");
366	class_destroy(drm_dp_aux_dev_class);
367}
368EXPORT_SYMBOL(drm_dp_aux_dev_exit);
v5.4
  1/*
  2 * Copyright © 2015 Intel Corporation
  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 (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21 * IN THE SOFTWARE.
 22 *
 23 * Authors:
 24 *    Rafael Antognolli <rafael.antognolli@intel.com>
 25 *
 26 */
 27
 28#include <linux/device.h>
 29#include <linux/fs.h>
 
 30#include <linux/init.h>
 31#include <linux/kernel.h>
 32#include <linux/module.h>
 33#include <linux/sched/signal.h>
 34#include <linux/slab.h>
 35#include <linux/uaccess.h>
 36#include <linux/uio.h>
 37
 38#include <drm/drm_crtc.h>
 39#include <drm/drm_dp_helper.h>
 40#include <drm/drm_dp_mst_helper.h>
 41#include <drm/drm_print.h>
 42
 43#include "drm_crtc_helper_internal.h"
 44
 45struct drm_dp_aux_dev {
 46	unsigned index;
 47	struct drm_dp_aux *aux;
 48	struct device *dev;
 49	struct kref refcount;
 50	atomic_t usecount;
 51};
 52
 53#define DRM_AUX_MINORS	256
 54#define AUX_MAX_OFFSET	(1 << 20)
 55static DEFINE_IDR(aux_idr);
 56static DEFINE_MUTEX(aux_idr_mutex);
 57static struct class *drm_dp_aux_dev_class;
 58static int drm_dev_major = -1;
 59
 60static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_minor(unsigned index)
 61{
 62	struct drm_dp_aux_dev *aux_dev = NULL;
 63
 64	mutex_lock(&aux_idr_mutex);
 65	aux_dev = idr_find(&aux_idr, index);
 66	if (!kref_get_unless_zero(&aux_dev->refcount))
 67		aux_dev = NULL;
 68	mutex_unlock(&aux_idr_mutex);
 69
 70	return aux_dev;
 71}
 72
 73static struct drm_dp_aux_dev *alloc_drm_dp_aux_dev(struct drm_dp_aux *aux)
 74{
 75	struct drm_dp_aux_dev *aux_dev;
 76	int index;
 77
 78	aux_dev = kzalloc(sizeof(*aux_dev), GFP_KERNEL);
 79	if (!aux_dev)
 80		return ERR_PTR(-ENOMEM);
 81	aux_dev->aux = aux;
 82	atomic_set(&aux_dev->usecount, 1);
 83	kref_init(&aux_dev->refcount);
 84
 85	mutex_lock(&aux_idr_mutex);
 86	index = idr_alloc(&aux_idr, aux_dev, 0, DRM_AUX_MINORS, GFP_KERNEL);
 
 87	mutex_unlock(&aux_idr_mutex);
 88	if (index < 0) {
 89		kfree(aux_dev);
 90		return ERR_PTR(index);
 91	}
 92	aux_dev->index = index;
 93
 94	return aux_dev;
 95}
 96
 97static void release_drm_dp_aux_dev(struct kref *ref)
 98{
 99	struct drm_dp_aux_dev *aux_dev =
100		container_of(ref, struct drm_dp_aux_dev, refcount);
101
102	kfree(aux_dev);
103}
104
105static ssize_t name_show(struct device *dev,
106			 struct device_attribute *attr, char *buf)
107{
108	ssize_t res;
109	struct drm_dp_aux_dev *aux_dev =
110		drm_dp_aux_dev_get_by_minor(MINOR(dev->devt));
111
112	if (!aux_dev)
113		return -ENODEV;
114
115	res = sprintf(buf, "%s\n", aux_dev->aux->name);
116	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
117
118	return res;
119}
120static DEVICE_ATTR_RO(name);
121
122static struct attribute *drm_dp_aux_attrs[] = {
123	&dev_attr_name.attr,
124	NULL,
125};
126ATTRIBUTE_GROUPS(drm_dp_aux);
127
128static int auxdev_open(struct inode *inode, struct file *file)
129{
130	unsigned int minor = iminor(inode);
131	struct drm_dp_aux_dev *aux_dev;
132
133	aux_dev = drm_dp_aux_dev_get_by_minor(minor);
134	if (!aux_dev)
135		return -ENODEV;
136
137	file->private_data = aux_dev;
138	return 0;
139}
140
141static loff_t auxdev_llseek(struct file *file, loff_t offset, int whence)
142{
143	return fixed_size_llseek(file, offset, whence, AUX_MAX_OFFSET);
144}
145
146static ssize_t auxdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
 
147{
148	struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
149	loff_t pos = iocb->ki_pos;
150	ssize_t res = 0;
151
152	if (!atomic_inc_not_zero(&aux_dev->usecount))
153		return -ENODEV;
154
155	iov_iter_truncate(to, AUX_MAX_OFFSET - pos);
156
157	while (iov_iter_count(to)) {
158		uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
159		ssize_t todo = min(iov_iter_count(to), sizeof(buf));
 
160
161		if (signal_pending(current)) {
162			res = -ERESTARTSYS;
163			break;
 
 
 
 
 
164		}
165
166		if (aux_dev->aux->is_remote)
167			res = drm_dp_mst_dpcd_read(aux_dev->aux, pos, buf,
168						   todo);
169		else
170			res = drm_dp_dpcd_read(aux_dev->aux, pos, buf, todo);
171
172		if (res <= 0)
173			break;
174
175		if (copy_to_iter(buf, res, to) != res) {
176			res = -EFAULT;
177			break;
178		}
179
180		pos += res;
 
 
181	}
182
183	if (pos != iocb->ki_pos)
184		res = pos - iocb->ki_pos;
185	iocb->ki_pos = pos;
186
187	if (atomic_dec_and_test(&aux_dev->usecount))
188		wake_up_var(&aux_dev->usecount);
189
190	return res;
191}
192
193static ssize_t auxdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
 
194{
195	struct drm_dp_aux_dev *aux_dev = iocb->ki_filp->private_data;
196	loff_t pos = iocb->ki_pos;
197	ssize_t res = 0;
198
199	if (!atomic_inc_not_zero(&aux_dev->usecount))
200		return -ENODEV;
201
202	iov_iter_truncate(from, AUX_MAX_OFFSET - pos);
203
204	while (iov_iter_count(from)) {
205		uint8_t buf[DP_AUX_MAX_PAYLOAD_BYTES];
206		ssize_t todo = min(iov_iter_count(from), sizeof(buf));
 
207
208		if (signal_pending(current)) {
209			res = -ERESTARTSYS;
210			break;
 
 
 
 
 
 
211		}
212
213		if (!copy_from_iter_full(buf, todo, from)) {
214			res = -EFAULT;
215			break;
 
216		}
217
218		if (aux_dev->aux->is_remote)
219			res = drm_dp_mst_dpcd_write(aux_dev->aux, pos, buf,
220						    todo);
221		else
222			res = drm_dp_dpcd_write(aux_dev->aux, pos, buf, todo);
223
224		if (res <= 0)
225			break;
226
227		pos += res;
228	}
229
230	if (pos != iocb->ki_pos)
231		res = pos - iocb->ki_pos;
232	iocb->ki_pos = pos;
233
234	if (atomic_dec_and_test(&aux_dev->usecount))
235		wake_up_var(&aux_dev->usecount);
236
237	return res;
238}
239
240static int auxdev_release(struct inode *inode, struct file *file)
241{
242	struct drm_dp_aux_dev *aux_dev = file->private_data;
243
244	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
245	return 0;
246}
247
248static const struct file_operations auxdev_fops = {
249	.owner		= THIS_MODULE,
250	.llseek		= auxdev_llseek,
251	.read_iter	= auxdev_read_iter,
252	.write_iter	= auxdev_write_iter,
253	.open		= auxdev_open,
254	.release	= auxdev_release,
255};
256
257#define to_auxdev(d) container_of(d, struct drm_dp_aux_dev, aux)
258
259static struct drm_dp_aux_dev *drm_dp_aux_dev_get_by_aux(struct drm_dp_aux *aux)
260{
261	struct drm_dp_aux_dev *iter, *aux_dev = NULL;
262	int id;
263
264	/* don't increase kref count here because this function should only be
265	 * used by drm_dp_aux_unregister_devnode. Thus, it will always have at
266	 * least one reference - the one that drm_dp_aux_register_devnode
267	 * created
268	 */
269	mutex_lock(&aux_idr_mutex);
270	idr_for_each_entry(&aux_idr, iter, id) {
271		if (iter->aux == aux) {
272			aux_dev = iter;
273			break;
274		}
275	}
276	mutex_unlock(&aux_idr_mutex);
277	return aux_dev;
278}
279
 
 
 
 
 
 
 
 
 
 
 
280void drm_dp_aux_unregister_devnode(struct drm_dp_aux *aux)
281{
282	struct drm_dp_aux_dev *aux_dev;
283	unsigned int minor;
284
285	aux_dev = drm_dp_aux_dev_get_by_aux(aux);
286	if (!aux_dev) /* attach must have failed */
287		return;
288
289	mutex_lock(&aux_idr_mutex);
290	idr_remove(&aux_idr, aux_dev->index);
291	mutex_unlock(&aux_idr_mutex);
292
293	atomic_dec(&aux_dev->usecount);
294	wait_var_event(&aux_dev->usecount, !atomic_read(&aux_dev->usecount));
 
295
296	minor = aux_dev->index;
297	if (aux_dev->dev)
298		device_destroy(drm_dp_aux_dev_class,
299			       MKDEV(drm_dev_major, minor));
300
301	DRM_DEBUG("drm_dp_aux_dev: aux [%s] unregistering\n", aux->name);
302	kref_put(&aux_dev->refcount, release_drm_dp_aux_dev);
303}
 
304
 
 
 
 
 
 
305int drm_dp_aux_register_devnode(struct drm_dp_aux *aux)
306{
307	struct drm_dp_aux_dev *aux_dev;
308	int res;
309
310	aux_dev = alloc_drm_dp_aux_dev(aux);
311	if (IS_ERR(aux_dev))
312		return PTR_ERR(aux_dev);
313
314	aux_dev->dev = device_create(drm_dp_aux_dev_class, aux->dev,
315				     MKDEV(drm_dev_major, aux_dev->index), NULL,
316				     "drm_dp_aux%d", aux_dev->index);
317	if (IS_ERR(aux_dev->dev)) {
318		res = PTR_ERR(aux_dev->dev);
319		aux_dev->dev = NULL;
320		goto error;
321	}
322
323	DRM_DEBUG("drm_dp_aux_dev: aux [%s] registered as minor %d\n",
324		  aux->name, aux_dev->index);
325	return 0;
326error:
327	drm_dp_aux_unregister_devnode(aux);
328	return res;
329}
 
330
331int drm_dp_aux_dev_init(void)
332{
333	int res;
334
335	drm_dp_aux_dev_class = class_create(THIS_MODULE, "drm_dp_aux_dev");
336	if (IS_ERR(drm_dp_aux_dev_class)) {
337		return PTR_ERR(drm_dp_aux_dev_class);
 
338	}
339	drm_dp_aux_dev_class->dev_groups = drm_dp_aux_groups;
340
341	res = register_chrdev(0, "aux", &auxdev_fops);
342	if (res < 0)
343		goto out;
344	drm_dev_major = res;
345
346	return 0;
347out:
348	class_destroy(drm_dp_aux_dev_class);
349	return res;
350}
 
351
352void drm_dp_aux_dev_exit(void)
353{
354	unregister_chrdev(drm_dev_major, "aux");
355	class_destroy(drm_dp_aux_dev_class);
356}