1 /* -*- C -*-
2 * main.c -- the bare sculld char module
3 *
4 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
5 * Copyright (C) 2001 O'Reilly & Associates
6 *
7 * The source code in this file can be freely used, adapted,
8 * and redistributed in source or binary form, so long as an
9 * acknowledgment appears in derived source files. The citation
10 * should list that the code comes from the book "Linux Device
11 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
12 * by O'Reilly & Associates. No warranty is attached;
13 * we cannot take responsibility for errors or fitness for use.
14 *
15 * $Id: _main.c.in,v 1.21 2004/10/14 20:11:39 corbet Exp $
16 */
17
18 #include <linux/config.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h> /* printk() */
23 #include <linux/slab.h> /* kmalloc() */
24 #include <linux/fs.h> /* everything... */
25 #include <linux/errno.h> /* error codes */
26 #include <linux/types.h> /* size_t */
27 #include <linux/proc_fs.h>
28 #include <linux/fcntl.h> /* O_ACCMODE */
29 #include <linux/aio.h>
30 #include <asm/uaccess.h>
31 #include "sculld.h" /* local definitions */
32
33
34 int sculld_major = SCULLD_MAJOR;
35 int sculld_devs = SCULLD_DEVS; /* number of bare sculld devices */
36 int sculld_qset = SCULLD_QSET;
37 int sculld_order = SCULLD_ORDER;
38
39 module_param(sculld_major, int, 0);
40 module_param(sculld_devs, int, 0);
41 module_param(sculld_qset, int, 0);
42 module_param(sculld_order, int, 0);
43 MODULE_AUTHOR("Alessandro Rubini");
44 MODULE_LICENSE("Dual BSD/GPL");
45
46 struct sculld_dev *sculld_devices; /* allocated in sculld_init */
47
48 int sculld_trim(struct sculld_dev *dev);
49 void sculld_cleanup(void);
50
51
52
53 /* Device model stuff */
54
55 static struct ldd_driver sculld_driver = {
56 .version = "$Revision: 1.21 $",
57 .module = THIS_MODULE,
58 .driver = {
59 .name = "sculld",
60 },
61 };
62
63
64
65 #ifdef SCULLD_USE_PROC /* don't waste space if unused */
66 /*
67 * The proc filesystem: function to read and entry
68 */
69
70 void sculld_proc_offset(char *buf, char **start, off_t *offset, int *len)
71 {
72 if (*offset == 0)
73 return;
74 if (*offset >= *len) {
75 /* Not there yet */
76 *offset -= *len;
77 *len = 0;
78 } else {
79 /* We're into the interesting stuff now */
80 *start = buf + *offset;
81 *offset = 0;
82 }
83 }
84
85 /* FIXME: Do we need this here?? It be ugly */
86 int sculld_read_procmem(char *buf, char **start, off_t offset,
87 int count, int *eof, void *data)
88 {
89 int i, j, order, qset, len = 0;
90 int limit = count - 80; /* Don't print more than this */
91 struct sculld_dev *d;
92
93 *start = buf;
94 for(i = 0; i < sculld_devs; i++) {
95 d = &sculld_devices[i];
96 if (down_interruptible (&d->sem))
97 return -ERESTARTSYS;
98 qset = d->qset; /* retrieve the features of each device */
99 order = d->order;
100 len += sprintf(buf+len,"\nDevice %i: qset %i, order %i, sz %li\n",
101 i, qset, order, (long)(d->size));
102 for (; d; d = d->next) { /* scan the list */
103 len += sprintf(buf+len," item at %p, qset at %p\n",d,d->data);
104 sculld_proc_offset (buf, start, &offset, &len);
105 if (len > limit)
106 goto out;
107 if (d->data && !d->next) /* dump only the last item - save space */
108 for (j = 0; j < qset; j++) {
109 if (d->data[j])
110 len += sprintf(buf+len," % 4i:%8p\n",j,d->data[j]);
111 sculld_proc_offset (buf, start, &offset, &len);
112 if (len > limit)
113 goto out;
114 }
115 }
116 out:
117 up (&sculld_devices[i].sem);
118 if (len > limit)
119 break;
120 }
121 *eof = 1;
122 return len;
123 }
124
125 #endif /* SCULLD_USE_PROC */
126
127 /*
128 * Open and close
129 */
130
131 int sculld_open (struct inode *inode, struct file *filp)
132 {
133 struct sculld_dev *dev; /* device information */
134
135 /* Find the device */
136 dev = container_of(inode->i_cdev, struct sculld_dev, cdev);
137
138 /* now trim to 0 the length of the device if open was write-only */
139 if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
140 if (down_interruptible (&dev->sem))
141 return -ERESTARTSYS;
142 sculld_trim(dev); /* ignore errors */
143 up (&dev->sem);
144 }
145
146 /* and use filp->private_data to point to the device data */
147 filp->private_data = dev;
148
149 return 0; /* success */
150 }
151
152 int sculld_release (struct inode *inode, struct file *filp)
153 {
154 return 0;
155 }
156
157 /*
158 * Follow the list
159 */
160 struct sculld_dev *sculld_follow(struct sculld_dev *dev, int n)
161 {
162 while (n--) {
163 if (!dev->next) {
164 dev->next = kmalloc(sizeof(struct sculld_dev), GFP_KERNEL);
165 memset(dev->next, 0, sizeof(struct sculld_dev));
166 }
167 dev = dev->next;
168 continue;
169 }
170 return dev;
171 }
172
173 /*
174 * Data management: read and write
175 */
176
177 ssize_t sculld_read (struct file *filp, char __user *buf, size_t count,
178 loff_t *f_pos)
179 {
180 struct sculld_dev *dev = filp->private_data; /* the first listitem */
181 struct sculld_dev *dptr;
182 int quantum = PAGE_SIZE << dev->order;
183 int qset = dev->qset;
184 int itemsize = quantum * qset; /* how many bytes in the listitem */
185 int item, s_pos, q_pos, rest;
186 ssize_t retval = 0;
187
188 if (down_interruptible (&dev->sem))
189 return -ERESTARTSYS;
190 if (*f_pos > dev->size)
191 goto nothing;
192 if (*f_pos + count > dev->size)
193 count = dev->size - *f_pos;
194 /* find listitem, qset index, and offset in the quantum */
195 item = ((long) *f_pos) / itemsize;
196 rest = ((long) *f_pos) % itemsize;
197 s_pos = rest / quantum; q_pos = rest % quantum;
198
199 /* follow the list up to the right position (defined elsewhere) */
200 dptr = sculld_follow(dev, item);
201
202 if (!dptr->data)
203 goto nothing; /* don't fill holes */
204 if (!dptr->data[s_pos])
205 goto nothing;
206 if (count > quantum - q_pos)
207 count = quantum - q_pos; /* read only up to the end of this quantum */
208
209 if (copy_to_user (buf, dptr->data[s_pos]+q_pos, count)) {
210 retval = -EFAULT;
211 goto nothing;
212 }
213 up (&dev->sem);
214
215 *f_pos += count;
216 return count;
217
218 nothing:
219 up (&dev->sem);
220 return retval;
221 }
222
223
224
225 ssize_t sculld_write (struct file *filp, const char __user *buf, size_t count,
226 loff_t *f_pos)
227 {
228 struct sculld_dev *dev = filp->private_data;
229 struct sculld_dev *dptr;
230 int quantum = PAGE_SIZE << dev->order;
231 int qset = dev->qset;
232 int itemsize = quantum * qset;
233 int item, s_pos, q_pos, rest;
234 ssize_t retval = -ENOMEM; /* our most likely error */
235
236 if (down_interruptible (&dev->sem))
237 return -ERESTARTSYS;
238
239 /* find listitem, qset index and offset in the quantum */
240 item = ((long) *f_pos) / itemsize;
241 rest = ((long) *f_pos) % itemsize;
242 s_pos = rest / quantum; q_pos = rest % quantum;
243
244 /* follow the list up to the right position */
245 dptr = sculld_follow(dev, item);
246 if (!dptr->data) {
247 dptr->data = kmalloc(qset * sizeof(void *), GFP_KERNEL);
248 if (!dptr->data)
249 goto nomem;
250 memset(dptr->data, 0, qset * sizeof(char *));
251 }
252 /* Here's the allocation of a single quantum */
253 if (!dptr->data[s_pos]) {
254 dptr->data[s_pos] =
255 (void *)__get_free_pages(GFP_KERNEL, dptr->order);
256 if (!dptr->data[s_pos])
257 goto nomem;
258 memset(dptr->data[s_pos], 0, PAGE_SIZE << dptr->order);
259 }
260 if (count > quantum - q_pos)
261 count = quantum - q_pos; /* write only up to the end of this quantum */
262 if (copy_from_user (dptr->data[s_pos]+q_pos, buf, count)) {
263 retval = -EFAULT;
264 goto nomem;
265 }
266 *f_pos += count;
267
268 /* update the size */
269 if (dev->size < *f_pos)
270 dev->size = *f_pos;
271 up (&dev->sem);
272 return count;
273
274 nomem:
275 up (&dev->sem);
276 return retval;
277 }
278
279 /*
280 * The ioctl() implementation
281 */
282
283 int sculld_ioctl (struct inode *inode, struct file *filp,
284 unsigned int cmd, unsigned long arg)
285 {
286
287 int err = 0, ret = 0, tmp;
288
289 /* don't even decode wrong cmds: better returning ENOTTY than EFAULT */
290 if (_IOC_TYPE(cmd) != SCULLD_IOC_MAGIC) return -ENOTTY;
291 if (_IOC_NR(cmd) > SCULLD_IOC_MAXNR) return -ENOTTY;
292
293 /*
294 * the type is a bitmask, and VERIFY_WRITE catches R/W
295 * transfers. Note that the type is user-oriented, while
296 * verify_area is kernel-oriented, so the concept of "read" and
297 * "write" is reversed
298 */
299 if (_IOC_DIR(cmd) & _IOC_READ)
300 err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
301 else if (_IOC_DIR(cmd) & _IOC_WRITE)
302 err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
303 if (err)
304 return -EFAULT;
305
306 switch(cmd) {
307
308 case SCULLD_IOCRESET:
309 sculld_qset = SCULLD_QSET;
310 sculld_order = SCULLD_ORDER;
311 break;
312
313 case SCULLD_IOCSORDER: /* Set: arg points to the value */
314 ret = __get_user(sculld_order, (int __user *) arg);
315 break;
316
317 case SCULLD_IOCTORDER: /* Tell: arg is the value */
318 sculld_order = arg;
319 break;
320
321 case SCULLD_IOCGORDER: /* Get: arg is pointer to result */
322 ret = __put_user (sculld_order, (int __user *) arg);
323 break;
324
325 case SCULLD_IOCQORDER: /* Query: return it (it's positive) */
326 return sculld_order;
327
328 case SCULLD_IOCXORDER: /* eXchange: use arg as pointer */
329 tmp = sculld_order;
330 ret = __get_user(sculld_order, (int __user *) arg);
331 if (ret == 0)
332 ret = __put_user(tmp, (int __user *) arg);
333 break;
334
335 case SCULLD_IOCHORDER: /* sHift: like Tell + Query */
336 tmp = sculld_order;
337 sculld_order = arg;
338 return tmp;
339
340 case SCULLD_IOCSQSET:
341 ret = __get_user(sculld_qset, (int __user *) arg);
342 break;
343
344 case SCULLD_IOCTQSET:
345 sculld_qset = arg;
346 break;
347
348 case SCULLD_IOCGQSET:
349 ret = __put_user(sculld_qset, (int __user *)arg);
350 break;
351
352 case SCULLD_IOCQQSET:
353 return sculld_qset;
354
355 case SCULLD_IOCXQSET:
356 tmp = sculld_qset;
357 ret = __get_user(sculld_qset, (int __user *)arg);
358 if (ret == 0)
359 ret = __put_user(tmp, (int __user *)arg);
360 break;
361
362 case SCULLD_IOCHQSET:
363 tmp = sculld_qset;
364 sculld_qset = arg;
365 return tmp;
366
367 default: /* redundant, as cmd was checked against MAXNR */
368 return -ENOTTY;
369 }
370
371 return ret;
372 }
373
374 /*
375 * The "extended" operations
376 */
377
378 loff_t sculld_llseek (struct file *filp, loff_t off, int whence)
379 {
380 struct sculld_dev *dev = filp->private_data;
381 long newpos;
382
383 switch(whence) {
384 case 0: /* SEEK_SET */
385 newpos = off;
386 break;
387
388 case 1: /* SEEK_CUR */
389 newpos = filp->f_pos + off;
390 break;
391
392 case 2: /* SEEK_END */
393 newpos = dev->size + off;
394 break;
395
396 default: /* can't happen */
397 return -EINVAL;
398 }
399 if (newpos<0) return -EINVAL;
400 filp->f_pos = newpos;
401 return newpos;
402 }
403
404
405 /*
406 * A simple asynchronous I/O implementation.
407 */
408
409 struct async_work {
410 struct kiocb *iocb;
411 int result;
412 struct work_struct work;
413 };
414
415 /*
416 * "Complete" an asynchronous operation.
417 */
418 static void sculld_do_deferred_op(void *p)
419 {
420 struct async_work *stuff = (struct async_work *) p;
421 aio_complete(stuff->iocb, stuff->result, 0);
422 kfree(stuff);
423 }
424
425
426 static int sculld_defer_op(int write, struct kiocb *iocb, char __user *buf,
427 size_t count, loff_t pos)
428 {
429 struct async_work *stuff;
430 int result;
431
432 /* Copy now while we can access the buffer */
433 if (write)
434 result = sculld_write(iocb->ki_filp, buf, count, &pos);
435 else
436 result = sculld_read(iocb->ki_filp, buf, count, &pos);
437
438 /* If this is a synchronous IOCB, we return our status now. */
439 if (is_sync_kiocb(iocb))
440 return result;
441
442 /* Otherwise defer the completion for a few milliseconds. */
443 stuff = kmalloc (sizeof (*stuff), GFP_KERNEL);
444 if (stuff == NULL)
445 return result; /* No memory, just complete now */
446 stuff->iocb = iocb;
447 stuff->result = result;
448 INIT_WORK(&stuff->work, sculld_do_deferred_op, stuff);
449 schedule_delayed_work(&stuff->work, HZ/100);
450 return -EIOCBQUEUED;
451 }
452
453
454 static ssize_t sculld_aio_read(struct kiocb *iocb, char __user *buf, size_t count,
455 loff_t pos)
456 {
457 return sculld_defer_op(0, iocb, buf, count, pos);
458 }
459
460 static ssize_t sculld_aio_write(struct kiocb *iocb, const char __user *buf,
461 size_t count, loff_t pos)
462 {
463 return sculld_defer_op(1, iocb, (char __user *) buf, count, pos);
464 }
465
466
467
468 /*
469 * Mmap *is* available, but confined in a different file
470 */
471 extern int sculld_mmap(struct file *filp, struct vm_area_struct *vma);
472
473
474 /*
475 * The fops
476 */
477
478 struct file_operations sculld_fops = {
479 .owner = THIS_MODULE,
480 .llseek = sculld_llseek,
481 .read = sculld_read,
482 .write = sculld_write,
483 .ioctl = sculld_ioctl,
484 .mmap = sculld_mmap,
485 .open = sculld_open,
486 .release = sculld_release,
487 .aio_read = sculld_aio_read,
488 .aio_write = sculld_aio_write,
489 };
490
491 int sculld_trim(struct sculld_dev *dev)
492 {
493 struct sculld_dev *next, *dptr;
494 int qset = dev->qset; /* "dev" is not-null */
495 int i;
496
497 if (dev->vmas) /* don't trim: there are active mappings */
498 return -EBUSY;
499
500 for (dptr = dev; dptr; dptr = next) { /* all the list items */
501 if (dptr->data) {
502 /* This code frees a whole quantum-set */
503 for (i = 0; i < qset; i++)
504 if (dptr->data[i])
505 free_pages((unsigned long)(dptr->data[i]),
506 dptr->order);
507
508 kfree(dptr->data);
509 dptr->data=NULL;
510 }
511 next=dptr->next;
512 if (dptr != dev) kfree(dptr); /* all of them but the first */
513 }
514 dev->size = 0;
515 dev->qset = sculld_qset;
516 dev->order = sculld_order;
517 dev->next = NULL;
518 return 0;
519 }
520
521
522 static void sculld_setup_cdev(struct sculld_dev *dev, int index)
523 {
524 int err, devno = MKDEV(sculld_major, index);
525
526 cdev_init(&dev->cdev, &sculld_fops);
527 dev->cdev.owner = THIS_MODULE;
528 dev->cdev.ops = &sculld_fops;
529 err = cdev_add (&dev->cdev, devno, 1);
530 /* Fail gracefully if need be */
531 if (err)
532 printk(KERN_NOTICE "Error %d adding scull%d", err, index);
533 }
534
535 static ssize_t sculld_show_dev(struct device *ddev, char *buf)
536 {
537 struct sculld_dev *dev = ddev->driver_data;
538
539 return print_dev_t(buf, dev->cdev.dev);
540 }
541
542 static DEVICE_ATTR(dev, S_IRUGO, sculld_show_dev, NULL);
543
544 static void sculld_register_dev(struct sculld_dev *dev, int index)
545 {
546 sprintf(dev->devname, "sculld%d", index);
547 dev->ldev.name = dev->devname;
548 dev->ldev.driver = &sculld_driver;
549 dev->ldev.dev.driver_data = dev;
550 register_ldd_device(&dev->ldev);
551 device_create_file(&dev->ldev.dev, &dev_attr_dev);
552 }
553
554
555 /*
556 * Finally, the module stuff
557 */
558
559 int sculld_init(void)
560 {
561 int result, i;
562 dev_t dev = MKDEV(sculld_major, 0);
563
564 /*
565 * Register your major, and accept a dynamic number.
566 */
567 if (sculld_major)
568 result = register_chrdev_region(dev, sculld_devs, "sculld");
569 else {
570 result = alloc_chrdev_region(&dev, 0, sculld_devs, "sculld");
571 sculld_major = MAJOR(dev);
572 }
573 if (result < 0)
574 return result;
575
576 /*
577 * Register with the driver core.
578 */
579 register_ldd_driver(&sculld_driver);
580
581 /*
582 * allocate the devices -- we can't have them static, as the number
583 * can be specified at load time
584 */
585 sculld_devices = kmalloc(sculld_devs*sizeof (struct sculld_dev), GFP_KERNEL);
586 if (!sculld_devices) {
587 result = -ENOMEM;
588 goto fail_malloc;
589 }
590 memset(sculld_devices, 0, sculld_devs*sizeof (struct sculld_dev));
591 for (i = 0; i < sculld_devs; i++) {
592 sculld_devices[i].order = sculld_order;
593 sculld_devices[i].qset = sculld_qset;
594 sema_init (&sculld_devices[i].sem, 1);
595 sculld_setup_cdev(sculld_devices + i, i);
596 sculld_register_dev(sculld_devices + i, i);
597 }
598
599
600 #ifdef SCULLD_USE_PROC /* only when available */
601 create_proc_read_entry("sculldmem", 0, NULL, sculld_read_procmem, NULL);
602 #endif
603 return 0; /* succeed */
604
605 fail_malloc:
606 unregister_chrdev_region(dev, sculld_devs);
607 return result;
608 }
609
610
611
612 void sculld_cleanup(void)
613 {
614 int i;
615
616 #ifdef SCULLD_USE_PROC
617 remove_proc_entry("sculldmem", NULL);
618 #endif
619
620 for (i = 0; i < sculld_devs; i++) {
621 unregister_ldd_device(&sculld_devices[i].ldev);
622 cdev_del(&sculld_devices[i].cdev);
623 sculld_trim(sculld_devices + i);
624 }
625 kfree(sculld_devices);
626 unregister_ldd_driver(&sculld_driver);
627 unregister_chrdev_region(MKDEV (sculld_major, 0), sculld_devs);
628 }
629
630
631 module_init(sculld_init);
632 module_exit(sculld_cleanup);
633
|
This page was automatically generated by the
LXR engine.
|