Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] media: lirc_dev: merge struct irctl into struct lirc_dev

The use of two separate structs (lirc_dev aka lirc_driver and irctl) makes
it much harder to follow the proper lifetime of the various structs and
necessitates hacks such as keeping a copy of struct lirc_dev inside
struct irctl.

Merging the two structs means that lirc_dev can properly manage the
lifetime of the resulting struct and simplifies the code at the same time.

[mchehab@s-opensource.com: fix merge conflict]
Signed-off-by: David Härdeman <david@hardeman.nu>
Signed-off-by: Sean Young <sean@mess.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

authored by

David Härdeman and committed by
Mauro Carvalho Chehab
b15e3937 13f96555

+177 -202
+7 -8
drivers/media/rc/ir-lirc-codec.c
··· 35 35 struct lirc_codec *lirc = &dev->raw->lirc; 36 36 int sample; 37 37 38 - if (!dev->raw->lirc.ldev || !dev->raw->lirc.ldev->rbuf) 38 + if (!dev->raw->lirc.ldev || !dev->raw->lirc.ldev->buf) 39 39 return -EINVAL; 40 40 41 41 /* Packet start */ ··· 84 84 (u64)LIRC_VALUE_MASK); 85 85 86 86 gap_sample = LIRC_SPACE(lirc->gap_duration); 87 - lirc_buffer_write(dev->raw->lirc.ldev->rbuf, 87 + lirc_buffer_write(dev->raw->lirc.ldev->buf, 88 88 (unsigned char *)&gap_sample); 89 89 lirc->gap = false; 90 90 } ··· 95 95 TO_US(ev.duration), TO_STR(ev.pulse)); 96 96 } 97 97 98 - lirc_buffer_write(dev->raw->lirc.ldev->rbuf, 98 + lirc_buffer_write(dev->raw->lirc.ldev->buf, 99 99 (unsigned char *) &sample); 100 - wake_up(&dev->raw->lirc.ldev->rbuf->wait_poll); 100 + wake_up(&dev->raw->lirc.ldev->buf->wait_poll); 101 101 102 102 return 0; 103 103 } ··· 384 384 dev->driver_name); 385 385 ldev->features = features; 386 386 ldev->data = &dev->raw->lirc; 387 - ldev->rbuf = NULL; 387 + ldev->buf = NULL; 388 388 ldev->code_length = sizeof(struct ir_raw_event) * 8; 389 389 ldev->chunk_size = sizeof(int); 390 390 ldev->buffer_size = LIRCBUF_SIZE; 391 391 ldev->fops = &lirc_fops; 392 - ldev->dev = &dev->dev; 392 + ldev->dev.parent = &dev->dev; 393 393 ldev->rdev = dev; 394 394 ldev->owner = THIS_MODULE; 395 395 ··· 402 402 return 0; 403 403 404 404 out: 405 - kfree(ldev); 405 + lirc_free_device(ldev); 406 406 return rc; 407 407 } 408 408 ··· 411 411 struct lirc_codec *lirc = &dev->raw->lirc; 412 412 413 413 lirc_unregister_device(lirc->ldev); 414 - kfree(lirc->ldev); 415 414 lirc->ldev = NULL; 416 415 417 416 return 0;
+141 -177
drivers/media/rc/lirc_dev.c
··· 34 34 35 35 static dev_t lirc_base_dev; 36 36 37 - struct irctl { 38 - struct lirc_dev d; 39 - bool attached; 40 - int open; 41 - 42 - struct mutex mutex; /* protect from simultaneous accesses */ 43 - struct lirc_buffer *buf; 44 - bool buf_internal; 45 - 46 - struct device dev; 47 - struct cdev cdev; 48 - }; 49 - 50 37 /* Used to keep track of allocated lirc devices */ 51 38 #define LIRC_MAX_DEVICES 256 52 39 static DEFINE_IDA(lirc_ida); ··· 41 54 /* Only used for sysfs but defined to void otherwise */ 42 55 static struct class *lirc_class; 43 56 44 - static void lirc_free_buffer(struct irctl *ir) 57 + static void lirc_release_device(struct device *ld) 45 58 { 46 - put_device(ir->dev.parent); 59 + struct lirc_dev *d = container_of(ld, struct lirc_dev, dev); 47 60 48 - if (ir->buf_internal) { 49 - lirc_buffer_free(ir->buf); 50 - kfree(ir->buf); 51 - ir->buf = NULL; 61 + put_device(d->dev.parent); 62 + 63 + if (d->buf_internal) { 64 + lirc_buffer_free(d->buf); 65 + kfree(d->buf); 66 + d->buf = NULL; 52 67 } 68 + kfree(d); 69 + module_put(THIS_MODULE); 53 70 } 54 71 55 - static void lirc_release(struct device *ld) 72 + static int lirc_allocate_buffer(struct lirc_dev *d) 56 73 { 57 - struct irctl *ir = container_of(ld, struct irctl, dev); 74 + int err; 58 75 59 - lirc_free_buffer(ir); 60 - kfree(ir); 61 - } 62 - 63 - static int lirc_allocate_buffer(struct irctl *ir) 64 - { 65 - int err = 0; 66 - struct lirc_dev *d = &ir->d; 67 - 68 - if (d->rbuf) { 69 - ir->buf = d->rbuf; 70 - ir->buf_internal = false; 71 - } else { 72 - ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); 73 - if (!ir->buf) { 74 - err = -ENOMEM; 75 - goto out; 76 - } 77 - 78 - err = lirc_buffer_init(ir->buf, d->chunk_size, d->buffer_size); 79 - if (err) { 80 - kfree(ir->buf); 81 - ir->buf = NULL; 82 - goto out; 83 - } 84 - 85 - ir->buf_internal = true; 86 - d->rbuf = ir->buf; 76 + if (d->buf) { 77 + d->buf_internal = false; 78 + return 0; 87 79 } 88 80 89 - out: 90 - return err; 81 + d->buf = kmalloc(sizeof(*d->buf), GFP_KERNEL); 82 + if (!d->buf) 83 + return -ENOMEM; 84 + 85 + err = lirc_buffer_init(d->buf, d->chunk_size, d->buffer_size); 86 + if (err) { 87 + kfree(d->buf); 88 + d->buf = NULL; 89 + return err; 90 + } 91 + 92 + d->buf_internal = true; 93 + return 0; 91 94 } 92 95 93 96 struct lirc_dev * 94 97 lirc_allocate_device(void) 95 98 { 96 - return kzalloc(sizeof(struct lirc_dev), GFP_KERNEL); 99 + struct lirc_dev *d; 100 + 101 + d = kzalloc(sizeof(*d), GFP_KERNEL); 102 + if (d) { 103 + mutex_init(&d->mutex); 104 + device_initialize(&d->dev); 105 + d->dev.class = lirc_class; 106 + d->dev.release = lirc_release_device; 107 + __module_get(THIS_MODULE); 108 + } 109 + 110 + return d; 97 111 } 98 112 EXPORT_SYMBOL(lirc_allocate_device); 99 113 100 114 void lirc_free_device(struct lirc_dev *d) 101 115 { 102 - kfree(d); 116 + if (!d) 117 + return; 118 + 119 + put_device(&d->dev); 103 120 } 104 121 EXPORT_SYMBOL(lirc_free_device); 105 122 106 123 int lirc_register_device(struct lirc_dev *d) 107 124 { 108 - struct irctl *ir; 109 125 int minor; 110 126 int err; 111 127 ··· 117 127 return -EBADRQC; 118 128 } 119 129 120 - if (!d->dev) { 121 - pr_err("dev pointer not filled in!\n"); 130 + if (!d->dev.parent) { 131 + pr_err("dev parent pointer not filled in!\n"); 122 132 return -EINVAL; 123 133 } 124 134 ··· 127 137 return -EINVAL; 128 138 } 129 139 130 - if (!d->rbuf && d->chunk_size < 1) { 140 + if (!d->buf && d->chunk_size < 1) { 131 141 pr_err("chunk_size must be set!\n"); 132 142 return -EINVAL; 133 143 } 134 144 135 - if (!d->rbuf && d->buffer_size < 1) { 145 + if (!d->buf && d->buffer_size < 1) { 136 146 pr_err("buffer_size must be set!\n"); 137 147 return -EINVAL; 138 148 } 139 149 140 150 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) { 141 - dev_err(d->dev, "code length must be less than %d bits\n", 142 - BUFLEN * 8); 151 + dev_err(&d->dev, "code length must be less than %d bits\n", 152 + BUFLEN * 8); 143 153 return -EBADRQC; 144 154 } 145 155 146 - if (!d->rbuf && !(d->fops && d->fops->read && 147 - d->fops->poll && d->fops->unlocked_ioctl)) { 148 - dev_err(d->dev, "undefined read, poll, ioctl\n"); 156 + if (!d->buf && !(d->fops && d->fops->read && 157 + d->fops->poll && d->fops->unlocked_ioctl)) { 158 + dev_err(&d->dev, "undefined read, poll, ioctl\n"); 149 159 return -EBADRQC; 150 160 } 151 161 ··· 155 165 if (d->features == 0) 156 166 d->features = LIRC_CAN_REC_LIRCCODE; 157 167 158 - ir = kzalloc(sizeof(*ir), GFP_KERNEL); 159 - if (!ir) 160 - return -ENOMEM; 161 - 162 - mutex_init(&ir->mutex); 163 - ir->d = *d; 164 - 165 168 if (LIRC_CAN_REC(d->features)) { 166 - err = lirc_allocate_buffer(ir); 167 - if (err) { 168 - kfree(ir); 169 + err = lirc_allocate_buffer(d); 170 + if (err) 169 171 return err; 170 - } 171 - d->rbuf = ir->buf; 172 172 } 173 173 174 174 minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL); 175 - if (minor < 0) { 176 - lirc_free_buffer(ir); 177 - kfree(ir); 175 + if (minor < 0) 178 176 return minor; 179 - } 180 177 181 - d->irctl = ir; 182 178 d->minor = minor; 183 - ir->d.minor = minor; 179 + d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor); 180 + dev_set_name(&d->dev, "lirc%d", d->minor); 184 181 185 - device_initialize(&ir->dev); 186 - ir->dev.devt = MKDEV(MAJOR(lirc_base_dev), ir->d.minor); 187 - ir->dev.class = lirc_class; 188 - ir->dev.parent = d->dev; 189 - ir->dev.release = lirc_release; 190 - dev_set_name(&ir->dev, "lirc%d", ir->d.minor); 182 + cdev_init(&d->cdev, d->fops); 183 + d->cdev.owner = d->owner; 184 + d->attached = true; 191 185 192 - cdev_init(&ir->cdev, d->fops); 193 - ir->cdev.owner = ir->d.owner; 194 - ir->attached = true; 195 - 196 - err = cdev_device_add(&ir->cdev, &ir->dev); 186 + err = cdev_device_add(&d->cdev, &d->dev); 197 187 if (err) { 198 188 ida_simple_remove(&lirc_ida, minor); 199 - put_device(&ir->dev); 200 189 return err; 201 190 } 202 191 203 - get_device(ir->dev.parent); 192 + get_device(d->dev.parent); 204 193 205 - dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", 206 - ir->d.name, ir->d.minor); 194 + dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n", 195 + d->name, d->minor); 207 196 208 197 return 0; 209 198 } ··· 190 221 191 222 void lirc_unregister_device(struct lirc_dev *d) 192 223 { 193 - struct irctl *ir; 194 - 195 - if (!d || !d->irctl) 224 + if (!d) 196 225 return; 197 226 198 - ir = d->irctl; 199 - 200 - dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", 227 + dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n", 201 228 d->name, d->minor); 202 229 203 - cdev_device_del(&ir->cdev, &ir->dev); 230 + mutex_lock(&d->mutex); 204 231 205 - mutex_lock(&ir->mutex); 206 - 207 - ir->attached = false; 208 - if (ir->open) { 209 - dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", 232 + d->attached = false; 233 + if (d->open) { 234 + dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n", 210 235 d->name, d->minor); 211 - wake_up_interruptible(&ir->buf->wait_poll); 236 + wake_up_interruptible(&d->buf->wait_poll); 212 237 } 213 238 214 - mutex_unlock(&ir->mutex); 239 + mutex_unlock(&d->mutex); 215 240 241 + cdev_device_del(&d->cdev, &d->dev); 216 242 ida_simple_remove(&lirc_ida, d->minor); 217 - put_device(&ir->dev); 243 + put_device(&d->dev); 218 244 } 219 245 EXPORT_SYMBOL(lirc_unregister_device); 220 246 221 247 int lirc_dev_fop_open(struct inode *inode, struct file *file) 222 248 { 223 - struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev); 249 + struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev); 224 250 int retval; 225 251 226 - dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); 252 + dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor); 227 253 228 - retval = mutex_lock_interruptible(&ir->mutex); 254 + retval = mutex_lock_interruptible(&d->mutex); 229 255 if (retval) 230 256 return retval; 231 257 232 - if (!ir->attached) { 258 + if (!d->attached) { 233 259 retval = -ENODEV; 234 260 goto out; 235 261 } 236 262 237 - if (ir->open) { 263 + if (d->open) { 238 264 retval = -EBUSY; 239 265 goto out; 240 266 } 241 267 242 - if (ir->d.rdev) { 243 - retval = rc_open(ir->d.rdev); 268 + if (d->rdev) { 269 + retval = rc_open(d->rdev); 244 270 if (retval) 245 271 goto out; 246 272 } 247 273 248 - if (ir->buf) 249 - lirc_buffer_clear(ir->buf); 274 + if (d->buf) 275 + lirc_buffer_clear(d->buf); 250 276 251 - ir->open++; 277 + d->open++; 252 278 253 279 lirc_init_pdata(inode, file); 254 280 nonseekable_open(inode, file); 255 - mutex_unlock(&ir->mutex); 281 + mutex_unlock(&d->mutex); 256 282 257 283 return 0; 258 284 259 285 out: 260 - mutex_unlock(&ir->mutex); 286 + mutex_unlock(&d->mutex); 261 287 return retval; 262 288 } 263 289 EXPORT_SYMBOL(lirc_dev_fop_open); 264 290 265 291 int lirc_dev_fop_close(struct inode *inode, struct file *file) 266 292 { 267 - struct irctl *ir = file->private_data; 293 + struct lirc_dev *d = file->private_data; 268 294 269 - mutex_lock(&ir->mutex); 295 + mutex_lock(&d->mutex); 270 296 271 - rc_close(ir->d.rdev); 272 - ir->open--; 297 + rc_close(d->rdev); 298 + d->open--; 273 299 274 - mutex_unlock(&ir->mutex); 300 + mutex_unlock(&d->mutex); 275 301 276 302 return 0; 277 303 } ··· 274 310 275 311 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) 276 312 { 277 - struct irctl *ir = file->private_data; 313 + struct lirc_dev *d = file->private_data; 278 314 unsigned int ret; 279 315 280 - if (!ir->attached) 316 + if (!d->attached) 281 317 return POLLHUP | POLLERR; 282 318 283 - if (ir->buf) { 284 - poll_wait(file, &ir->buf->wait_poll, wait); 319 + if (d->buf) { 320 + poll_wait(file, &d->buf->wait_poll, wait); 285 321 286 - if (lirc_buffer_empty(ir->buf)) 322 + if (lirc_buffer_empty(d->buf)) 287 323 ret = 0; 288 324 else 289 325 ret = POLLIN | POLLRDNORM; 290 - } else 326 + } else { 291 327 ret = POLLERR; 328 + } 292 329 293 - dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", 294 - ir->d.name, ir->d.minor, ret); 330 + dev_dbg(&d->dev, LOGHEAD "poll result = %d\n", d->name, d->minor, ret); 295 331 296 332 return ret; 297 333 } ··· 299 335 300 336 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 301 337 { 302 - struct irctl *ir = file->private_data; 338 + struct lirc_dev *d = file->private_data; 303 339 __u32 mode; 304 340 int result; 305 341 306 - dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", 307 - ir->d.name, ir->d.minor, cmd); 342 + dev_dbg(&d->dev, LOGHEAD "ioctl called (0x%x)\n", 343 + d->name, d->minor, cmd); 308 344 309 - result = mutex_lock_interruptible(&ir->mutex); 345 + result = mutex_lock_interruptible(&d->mutex); 310 346 if (result) 311 347 return result; 312 348 313 - if (!ir->attached) { 349 + if (!d->attached) { 314 350 result = -ENODEV; 315 351 goto out; 316 352 } 317 353 318 354 switch (cmd) { 319 355 case LIRC_GET_FEATURES: 320 - result = put_user(ir->d.features, (__u32 __user *)arg); 356 + result = put_user(d->features, (__u32 __user *)arg); 321 357 break; 322 358 case LIRC_GET_REC_MODE: 323 - if (!LIRC_CAN_REC(ir->d.features)) { 359 + if (!LIRC_CAN_REC(d->features)) { 324 360 result = -ENOTTY; 325 361 break; 326 362 } 327 363 328 364 result = put_user(LIRC_REC2MODE 329 - (ir->d.features & LIRC_CAN_REC_MASK), 365 + (d->features & LIRC_CAN_REC_MASK), 330 366 (__u32 __user *)arg); 331 367 break; 332 368 case LIRC_SET_REC_MODE: 333 - if (!LIRC_CAN_REC(ir->d.features)) { 369 + if (!LIRC_CAN_REC(d->features)) { 334 370 result = -ENOTTY; 335 371 break; 336 372 } 337 373 338 374 result = get_user(mode, (__u32 __user *)arg); 339 - if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) 375 + if (!result && !(LIRC_MODE2REC(mode) & d->features)) 340 376 result = -EINVAL; 341 377 /* 342 378 * FIXME: We should actually set the mode somehow but ··· 344 380 */ 345 381 break; 346 382 case LIRC_GET_LENGTH: 347 - result = put_user(ir->d.code_length, (__u32 __user *)arg); 383 + result = put_user(d->code_length, (__u32 __user *)arg); 348 384 break; 349 385 case LIRC_GET_MIN_TIMEOUT: 350 - if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 351 - ir->d.min_timeout == 0) { 386 + if (!(d->features & LIRC_CAN_SET_REC_TIMEOUT) || 387 + d->min_timeout == 0) { 352 388 result = -ENOTTY; 353 389 break; 354 390 } 355 391 356 - result = put_user(ir->d.min_timeout, (__u32 __user *)arg); 392 + result = put_user(d->min_timeout, (__u32 __user *)arg); 357 393 break; 358 394 case LIRC_GET_MAX_TIMEOUT: 359 - if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 360 - ir->d.max_timeout == 0) { 395 + if (!(d->features & LIRC_CAN_SET_REC_TIMEOUT) || 396 + d->max_timeout == 0) { 361 397 result = -ENOTTY; 362 398 break; 363 399 } 364 400 365 - result = put_user(ir->d.max_timeout, (__u32 __user *)arg); 401 + result = put_user(d->max_timeout, (__u32 __user *)arg); 366 402 break; 367 403 default: 368 404 result = -ENOTTY; 369 405 } 370 406 371 407 out: 372 - mutex_unlock(&ir->mutex); 408 + mutex_unlock(&d->mutex); 373 409 return result; 374 410 } 375 411 EXPORT_SYMBOL(lirc_dev_fop_ioctl); ··· 379 415 size_t length, 380 416 loff_t *ppos) 381 417 { 382 - struct irctl *ir = file->private_data; 418 + struct lirc_dev *d = file->private_data; 383 419 unsigned char *buf; 384 420 int ret, written = 0; 385 421 DECLARE_WAITQUEUE(wait, current); 386 422 387 - dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); 388 - 389 - buf = kzalloc(ir->buf->chunk_size, GFP_KERNEL); 423 + buf = kzalloc(d->buf->chunk_size, GFP_KERNEL); 390 424 if (!buf) 391 425 return -ENOMEM; 392 426 393 - ret = mutex_lock_interruptible(&ir->mutex); 427 + dev_dbg(&d->dev, LOGHEAD "read called\n", d->name, d->minor); 428 + 429 + ret = mutex_lock_interruptible(&d->mutex); 394 430 if (ret) { 395 431 kfree(buf); 396 432 return ret; 397 433 } 398 434 399 - if (!ir->attached) { 435 + if (!d->attached) { 400 436 ret = -ENODEV; 401 437 goto out_locked; 402 438 } 403 439 404 - if (!LIRC_CAN_REC(ir->d.features)) { 440 + if (!LIRC_CAN_REC(d->features)) { 405 441 ret = -EINVAL; 406 442 goto out_locked; 407 443 } 408 444 409 - if (length % ir->buf->chunk_size) { 445 + if (length % d->buf->chunk_size) { 410 446 ret = -EINVAL; 411 447 goto out_locked; 412 448 } ··· 416 452 * to avoid losing scan code (in case when queue is awaken somewhere 417 453 * between while condition checking and scheduling) 418 454 */ 419 - add_wait_queue(&ir->buf->wait_poll, &wait); 455 + add_wait_queue(&d->buf->wait_poll, &wait); 420 456 421 457 /* 422 458 * while we didn't provide 'length' bytes, device is opened in blocking 423 459 * mode and 'copy_to_user' is happy, wait for data. 424 460 */ 425 461 while (written < length && ret == 0) { 426 - if (lirc_buffer_empty(ir->buf)) { 462 + if (lirc_buffer_empty(d->buf)) { 427 463 /* According to the read(2) man page, 'written' can be 428 464 * returned as less than 'length', instead of blocking 429 465 * again, returning -EWOULDBLOCK, or returning ··· 440 476 break; 441 477 } 442 478 443 - mutex_unlock(&ir->mutex); 479 + mutex_unlock(&d->mutex); 444 480 set_current_state(TASK_INTERRUPTIBLE); 445 481 schedule(); 446 482 set_current_state(TASK_RUNNING); 447 483 448 - ret = mutex_lock_interruptible(&ir->mutex); 484 + ret = mutex_lock_interruptible(&d->mutex); 449 485 if (ret) { 450 - remove_wait_queue(&ir->buf->wait_poll, &wait); 486 + remove_wait_queue(&d->buf->wait_poll, &wait); 451 487 goto out_unlocked; 452 488 } 453 489 454 - if (!ir->attached) { 490 + if (!d->attached) { 455 491 ret = -ENODEV; 456 492 goto out_locked; 457 493 } 458 494 } else { 459 - lirc_buffer_read(ir->buf, buf); 495 + lirc_buffer_read(d->buf, buf); 460 496 ret = copy_to_user((void __user *)buffer+written, buf, 461 - ir->buf->chunk_size); 497 + d->buf->chunk_size); 462 498 if (!ret) 463 - written += ir->buf->chunk_size; 499 + written += d->buf->chunk_size; 464 500 else 465 501 ret = -EFAULT; 466 502 } 467 503 } 468 504 469 - remove_wait_queue(&ir->buf->wait_poll, &wait); 505 + remove_wait_queue(&d->buf->wait_poll, &wait); 470 506 471 507 out_locked: 472 - mutex_unlock(&ir->mutex); 508 + mutex_unlock(&d->mutex); 473 509 474 510 out_unlocked: 475 511 kfree(buf); ··· 480 516 481 517 void lirc_init_pdata(struct inode *inode, struct file *file) 482 518 { 483 - struct irctl *ir = container_of(inode->i_cdev, struct irctl, cdev); 519 + struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev); 484 520 485 - file->private_data = ir; 521 + file->private_data = d; 486 522 } 487 523 EXPORT_SYMBOL(lirc_init_pdata); 488 524 489 525 void *lirc_get_pdata(struct file *file) 490 526 { 491 - struct irctl *ir = file->private_data; 527 + struct lirc_dev *d = file->private_data; 492 528 493 - return ir->d.data; 529 + return d->data; 494 530 } 495 531 EXPORT_SYMBOL(lirc_get_pdata); 496 532
+9 -11
drivers/staging/media/lirc/lirc_zilog.c
··· 184 184 * ir->open_count == 0 - happens on final close() 185 185 * ir_lock, tx_ref_lock, rx_ref_lock, all released 186 186 */ 187 - if (ir->l) { 187 + if (ir->l) 188 188 lirc_unregister_device(ir->l); 189 - lirc_free_device(ir->l); 190 - } 191 189 192 190 if (kfifo_initialized(&ir->rbuf.fifo)) 193 191 lirc_buffer_free(&ir->rbuf); ··· 316 318 int ret; 317 319 int failures = 0; 318 320 unsigned char sendbuf[1] = { 0 }; 319 - struct lirc_buffer *rbuf = ir->l->rbuf; 321 + struct lirc_buffer *rbuf = ir->l->buf; 320 322 struct IR_rx *rx; 321 323 struct IR_tx *tx; 322 324 ··· 462 464 static int lirc_thread(void *arg) 463 465 { 464 466 struct IR *ir = arg; 465 - struct lirc_buffer *rbuf = ir->l->rbuf; 467 + struct lirc_buffer *rbuf = ir->l->buf; 466 468 467 469 dev_dbg(ir->dev, "poll thread started\n"); 468 470 ··· 883 885 { 884 886 struct IR *ir = lirc_get_pdata(filep); 885 887 struct IR_rx *rx; 886 - struct lirc_buffer *rbuf = ir->l->rbuf; 888 + struct lirc_buffer *rbuf = ir->l->buf; 887 889 int ret = 0, written = 0, retries = 0; 888 890 unsigned int m; 889 891 DECLARE_WAITQUEUE(wait, current); ··· 1201 1203 { 1202 1204 struct IR *ir = lirc_get_pdata(filep); 1203 1205 struct IR_rx *rx; 1204 - struct lirc_buffer *rbuf = ir->l->rbuf; 1206 + struct lirc_buffer *rbuf = ir->l->buf; 1205 1207 unsigned int ret; 1206 1208 1207 1209 dev_dbg(ir->dev, "%s called\n", __func__); ··· 1447 1449 ir->l->code_length = 13; 1448 1450 ir->l->fops = &lirc_fops; 1449 1451 ir->l->owner = THIS_MODULE; 1452 + ir->l->dev.parent = &adap->dev; 1450 1453 1451 1454 /* 1452 1455 * FIXME this is a pointer reference to us, but no refcount. ··· 1455 1456 * This OK for now, since lirc_dev currently won't touch this 1456 1457 * buffer as we provide our own lirc_fops. 1457 1458 * 1458 - * Currently our own lirc_fops rely on this ir->l->rbuf pointer 1459 + * Currently our own lirc_fops rely on this ir->l->buf pointer 1459 1460 */ 1460 - ir->l->rbuf = &ir->rbuf; 1461 - ir->l->dev = &adap->dev; 1461 + ir->l->buf = &ir->rbuf; 1462 1462 /* This will be returned by lirc_get_pdata() */ 1463 1463 ir->l->data = ir; 1464 - ret = lirc_buffer_init(ir->l->rbuf, 2, BUFLEN / 2); 1464 + ret = lirc_buffer_init(ir->l->buf, 2, BUFLEN / 2); 1465 1465 if (ret) { 1466 1466 lirc_free_device(ir->l); 1467 1467 ir->l = NULL;
+20 -6
include/media/lirc_dev.h
··· 17 17 #include <linux/poll.h> 18 18 #include <linux/kfifo.h> 19 19 #include <media/lirc.h> 20 + #include <linux/device.h> 21 + #include <linux/cdev.h> 20 22 21 23 struct lirc_buffer { 22 24 wait_queue_head_t wait_poll; ··· 129 127 * LIRC_CAN_SET_REC_TIMEOUT is defined. 130 128 * @max_timeout: Maximum timeout for record. Valid only if 131 129 * LIRC_CAN_SET_REC_TIMEOUT is defined. 132 - * @rbuf: if not NULL, it will be used as a read buffer, you will 130 + * @buf: if %NULL, lirc_dev will allocate and manage the buffer, 131 + * otherwise allocated by the caller which will 133 132 * have to write to the buffer by other means, like irq's 134 133 * (see also lirc_serial.c). 134 + * @buf_internal: whether lirc_dev has allocated the read buffer or not 135 135 * @rdev: &struct rc_dev associated with the device 136 136 * @fops: &struct file_operations for the device 137 - * @dev: &struct device assigned to the device 138 137 * @owner: the module owning this struct 139 - * @irctl: &struct irctl assigned to the device 138 + * @attached: if the device is still live 139 + * @open: open count for the device's chardev 140 + * @mutex: serialises file_operations calls 141 + * @dev: &struct device assigned to the device 142 + * @cdev: &struct cdev assigned to the device 140 143 */ 141 144 struct lirc_dev { 142 145 char name[40]; ··· 151 144 152 145 unsigned int buffer_size; /* in chunks holding one code each */ 153 146 unsigned int chunk_size; 147 + struct lirc_buffer *buf; 148 + bool buf_internal; 154 149 155 150 void *data; 156 151 int min_timeout; 157 152 int max_timeout; 158 - struct lirc_buffer *rbuf; 159 153 struct rc_dev *rdev; 160 154 const struct file_operations *fops; 161 - struct device *dev; 162 155 struct module *owner; 163 - struct irctl *irctl; 156 + 157 + bool attached; 158 + int open; 159 + 160 + struct mutex mutex; /* protect from simultaneous accesses */ 161 + 162 + struct device dev; 163 + struct cdev cdev; 164 164 }; 165 165 166 166 struct lirc_dev *lirc_allocate_device(void);