blob: 383a95f34a0da4e8adec682df992786d572efe27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux/SPARC PROM Configuration Driver
3 * Copyright (C) 1996 Thomas K. Dyas (tdyas@noc.rutgers.edu)
4 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
5 *
6 * This character device driver allows user programs to access the
7 * PROM device tree. It is compatible with the SunOS /dev/openprom
8 * driver and the NetBSD /dev/openprom driver. The SunOS eeprom
9 * utility works without any modifications.
10 *
11 * The driver uses a minor number under the misc device major. The
12 * file read/write mode determines the type of access to the PROM.
13 * Interrupts are disabled whenever the driver calls into the PROM for
14 * sanity's sake.
15 */
16
17/* This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of the
20 * License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32#define PROMLIB_INTERNAL
33
34#include <linux/config.h>
35#include <linux/module.h>
36#include <linux/kernel.h>
37#include <linux/sched.h>
38#include <linux/errno.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/miscdevice.h>
Christoph Hellwigb31023f2005-11-07 14:12:47 -080042#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/init.h>
44#include <linux/fs.h>
45#include <asm/oplib.h>
46#include <asm/system.h>
47#include <asm/uaccess.h>
48#include <asm/openpromio.h>
49#ifdef CONFIG_PCI
50#include <linux/pci.h>
51#include <asm/pbm.h>
52#endif
53
54/* Private data kept by the driver for each descriptor. */
55typedef struct openprom_private_data
56{
57 int current_node; /* Current node for SunOS ioctls. */
58 int lastnode; /* Last valid node used by BSD ioctls. */
59} DATA;
60
61/* ID of the PROM node containing all of the EEPROM options. */
62static int options_node = 0;
63
64/*
65 * Copy an openpromio structure into kernel space from user space.
66 * This routine does error checking to make sure that all memory
67 * accesses are within bounds. A pointer to the allocated openpromio
68 * structure will be placed in "*opp_p". Return value is the length
69 * of the user supplied buffer.
70 */
71static int copyin(struct openpromio __user *info, struct openpromio **opp_p)
72{
73 unsigned int bufsize;
74
75 if (!info || !opp_p)
76 return -EFAULT;
77
78 if (get_user(bufsize, &info->oprom_size))
79 return -EFAULT;
80
81 if (bufsize == 0)
82 return -EINVAL;
83
84 /* If the bufsize is too large, just limit it.
85 * Fix from Jason Rappleye.
86 */
87 if (bufsize > OPROMMAXPARAM)
88 bufsize = OPROMMAXPARAM;
89
90 if (!(*opp_p = kmalloc(sizeof(int) + bufsize + 1, GFP_KERNEL)))
91 return -ENOMEM;
92 memset(*opp_p, 0, sizeof(int) + bufsize + 1);
93
94 if (copy_from_user(&(*opp_p)->oprom_array,
95 &info->oprom_array, bufsize)) {
96 kfree(*opp_p);
97 return -EFAULT;
98 }
99 return bufsize;
100}
101
102static int getstrings(struct openpromio __user *info, struct openpromio **opp_p)
103{
104 int n, bufsize;
105 char c;
106
107 if (!info || !opp_p)
108 return -EFAULT;
109
110 if (!(*opp_p = kmalloc(sizeof(int) + OPROMMAXPARAM + 1, GFP_KERNEL)))
111 return -ENOMEM;
112
113 memset(*opp_p, 0, sizeof(int) + OPROMMAXPARAM + 1);
114 (*opp_p)->oprom_size = 0;
115
116 n = bufsize = 0;
117 while ((n < 2) && (bufsize < OPROMMAXPARAM)) {
118 if (get_user(c, &info->oprom_array[bufsize])) {
119 kfree(*opp_p);
120 return -EFAULT;
121 }
122 if (c == '\0')
123 n++;
124 (*opp_p)->oprom_array[bufsize++] = c;
125 }
126 if (!n) {
127 kfree(*opp_p);
128 return -EINVAL;
129 }
130 return bufsize;
131}
132
133/*
134 * Copy an openpromio structure in kernel space back to user space.
135 */
136static int copyout(void __user *info, struct openpromio *opp, int len)
137{
138 if (copy_to_user(info, opp, len))
139 return -EFAULT;
140 return 0;
141}
142
143/*
144 * SunOS and Solaris /dev/openprom ioctl calls.
145 */
146static int openprom_sunos_ioctl(struct inode * inode, struct file * file,
147 unsigned int cmd, unsigned long arg, int node)
148{
149 DATA *data = (DATA *) file->private_data;
150 char buffer[OPROMMAXPARAM+1], *buf;
151 struct openpromio *opp;
152 int bufsize, len, error = 0;
153 static int cnt;
154 void __user *argp = (void __user *)arg;
155
156 if (cmd == OPROMSETOPT)
157 bufsize = getstrings(argp, &opp);
158 else
159 bufsize = copyin(argp, &opp);
160
161 if (bufsize < 0)
162 return bufsize;
163
164 switch (cmd) {
165 case OPROMGETOPT:
166 case OPROMGETPROP:
167 len = prom_getproplen(node, opp->oprom_array);
168
169 if (len <= 0 || len > bufsize) {
170 error = copyout(argp, opp, sizeof(int));
171 break;
172 }
173
174 len = prom_getproperty(node, opp->oprom_array, buffer, bufsize);
175
176 memcpy(opp->oprom_array, buffer, len);
177 opp->oprom_array[len] = '\0';
178 opp->oprom_size = len;
179
180 error = copyout(argp, opp, sizeof(int) + bufsize);
181 break;
182
183 case OPROMNXTOPT:
184 case OPROMNXTPROP:
185 buf = prom_nextprop(node, opp->oprom_array, buffer);
186
187 len = strlen(buf);
188 if (len == 0 || len + 1 > bufsize) {
189 error = copyout(argp, opp, sizeof(int));
190 break;
191 }
192
193 memcpy(opp->oprom_array, buf, len);
194 opp->oprom_array[len] = '\0';
195 opp->oprom_size = ++len;
196
197 error = copyout(argp, opp, sizeof(int) + bufsize);
198 break;
199
200 case OPROMSETOPT:
201 case OPROMSETOPT2:
202 buf = opp->oprom_array + strlen(opp->oprom_array) + 1;
203 len = opp->oprom_array + bufsize - buf;
204
205 error = prom_setprop(options_node, opp->oprom_array,
206 buf, len);
207
208 if (error < 0)
209 error = -EINVAL;
210 break;
211
212 case OPROMNEXT:
213 case OPROMCHILD:
214 case OPROMSETCUR:
215 if (bufsize < sizeof(int)) {
216 error = -EINVAL;
217 break;
218 }
219
220 node = *((int *) opp->oprom_array);
221
222 switch (cmd) {
223 case OPROMNEXT: node = __prom_getsibling(node); break;
224 case OPROMCHILD: node = __prom_getchild(node); break;
225 case OPROMSETCUR: break;
226 }
227
228 data->current_node = node;
229 *((int *)opp->oprom_array) = node;
230 opp->oprom_size = sizeof(int);
231
232 error = copyout(argp, opp, bufsize + sizeof(int));
233 break;
234
235 case OPROMPCI2NODE:
236 error = -EINVAL;
237
238 if (bufsize >= 2*sizeof(int)) {
239#ifdef CONFIG_PCI
240 struct pci_dev *pdev;
241 struct pcidev_cookie *pcp;
242 pdev = pci_find_slot (((int *) opp->oprom_array)[0],
243 ((int *) opp->oprom_array)[1]);
244
245 pcp = pdev->sysdata;
246 if (pcp != NULL && pcp->prom_node != -1 && pcp->prom_node) {
247 node = pcp->prom_node;
248 data->current_node = node;
249 *((int *)opp->oprom_array) = node;
250 opp->oprom_size = sizeof(int);
251 error = copyout(argp, opp, bufsize + sizeof(int));
252 }
253#endif
254 }
255 break;
256
257 case OPROMPATH2NODE:
258 node = prom_finddevice(opp->oprom_array);
259 data->current_node = node;
260 *((int *)opp->oprom_array) = node;
261 opp->oprom_size = sizeof(int);
262
263 error = copyout(argp, opp, bufsize + sizeof(int));
264 break;
265
266 case OPROMGETBOOTARGS:
267 buf = saved_command_line;
268
269 len = strlen(buf);
270
271 if (len > bufsize) {
272 error = -EINVAL;
273 break;
274 }
275
276 strcpy(opp->oprom_array, buf);
277 opp->oprom_size = len;
278
279 error = copyout(argp, opp, bufsize + sizeof(int));
280 break;
281
282 case OPROMU2P:
283 case OPROMGETCONS:
284 case OPROMGETFBNAME:
285 if (cnt++ < 10)
286 printk(KERN_INFO "openprom_sunos_ioctl: unimplemented ioctl\n");
287 error = -EINVAL;
288 break;
289 default:
290 if (cnt++ < 10)
291 printk(KERN_INFO "openprom_sunos_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
292 error = -EINVAL;
293 break;
294 }
295
296 kfree(opp);
297 return error;
298}
299
300
301/* Return nonzero if a specific node is in the PROM device tree. */
302static int intree(int root, int node)
303{
304 for (; root != 0; root = prom_getsibling(root))
305 if (root == node || intree(prom_getchild(root),node))
306 return 1;
307 return 0;
308}
309
310/* Return nonzero if a specific node is "valid". */
311static int goodnode(int n, DATA *data)
312{
313 if (n == data->lastnode || n == prom_root_node || n == options_node)
314 return 1;
315 if (n == 0 || n == -1 || !intree(prom_root_node,n))
316 return 0;
317 data->lastnode = n;
318 return 1;
319}
320
321/* Copy in a whole string from userspace into kernelspace. */
322static int copyin_string(char __user *user, size_t len, char **ptr)
323{
324 char *tmp;
325
326 if ((ssize_t)len < 0 || (ssize_t)(len + 1) < 0)
327 return -EINVAL;
328
329 tmp = kmalloc(len + 1, GFP_KERNEL);
330 if (!tmp)
331 return -ENOMEM;
332
333 if(copy_from_user(tmp, user, len)) {
334 kfree(tmp);
335 return -EFAULT;
336 }
337
338 tmp[len] = '\0';
339
340 *ptr = tmp;
341
342 return 0;
343}
344
345/*
346 * NetBSD /dev/openprom ioctl calls.
347 */
348static int openprom_bsd_ioctl(struct inode * inode, struct file * file,
349 unsigned int cmd, unsigned long arg)
350{
351 DATA *data = (DATA *) file->private_data;
352 void __user *argp = (void __user *)arg;
353 struct opiocdesc op;
354 int error, node, len;
355 char *str, *tmp;
356 char buffer[64];
357 static int cnt;
358
359 switch (cmd) {
360 case OPIOCGET:
361 if (copy_from_user(&op, argp, sizeof(op)))
362 return -EFAULT;
363
364 if (!goodnode(op.op_nodeid,data))
365 return -EINVAL;
366
367 error = copyin_string(op.op_name, op.op_namelen, &str);
368 if (error)
369 return error;
370
371 len = prom_getproplen(op.op_nodeid,str);
372
373 if (len > op.op_buflen) {
374 kfree(str);
375 return -ENOMEM;
376 }
377
378 op.op_buflen = len;
379
380 if (len <= 0) {
381 kfree(str);
382 /* Verified by the above copy_from_user */
383 if (__copy_to_user(argp, &op,
384 sizeof(op)))
385 return -EFAULT;
386 return 0;
387 }
388
389 tmp = kmalloc(len + 1, GFP_KERNEL);
390 if (!tmp) {
391 kfree(str);
392 return -ENOMEM;
393 }
394
395 prom_getproperty(op.op_nodeid, str, tmp, len);
396
397 tmp[len] = '\0';
398
399 if (__copy_to_user(argp, &op, sizeof(op)) != 0
400 || copy_to_user(op.op_buf, tmp, len) != 0)
401 error = -EFAULT;
402
403 kfree(tmp);
404 kfree(str);
405
406 return error;
407
408 case OPIOCNEXTPROP:
409 if (copy_from_user(&op, argp, sizeof(op)))
410 return -EFAULT;
411
412 if (!goodnode(op.op_nodeid,data))
413 return -EINVAL;
414
415 error = copyin_string(op.op_name, op.op_namelen, &str);
416 if (error)
417 return error;
418
419 tmp = prom_nextprop(op.op_nodeid,str,buffer);
420
421 if (tmp) {
422 len = strlen(tmp);
423 if (len > op.op_buflen)
424 len = op.op_buflen;
425 else
426 op.op_buflen = len;
427 } else {
428 len = op.op_buflen = 0;
429 }
430
431 if (!access_ok(VERIFY_WRITE, argp, sizeof(op))) {
432 kfree(str);
433 return -EFAULT;
434 }
435
436 if (!access_ok(VERIFY_WRITE, op.op_buf, len)) {
437 kfree(str);
438 return -EFAULT;
439 }
440
441 error = __copy_to_user(argp, &op, sizeof(op));
442 if (!error) error = __copy_to_user(op.op_buf, tmp, len);
443
444 kfree(str);
445
446 return error;
447
448 case OPIOCSET:
449 if (copy_from_user(&op, argp, sizeof(op)))
450 return -EFAULT;
451
452 if (!goodnode(op.op_nodeid,data))
453 return -EINVAL;
454
455 error = copyin_string(op.op_name, op.op_namelen, &str);
456 if (error)
457 return error;
458
459 error = copyin_string(op.op_buf, op.op_buflen, &tmp);
460 if (error) {
461 kfree(str);
462 return error;
463 }
464
465 len = prom_setprop(op.op_nodeid,str,tmp,op.op_buflen+1);
466
467 if (len != op.op_buflen)
468 return -EINVAL;
469
470 kfree(str);
471 kfree(tmp);
472
473 return 0;
474
475 case OPIOCGETOPTNODE:
476 if (copy_to_user(argp, &options_node, sizeof(int)))
477 return -EFAULT;
478 return 0;
479
480 case OPIOCGETNEXT:
481 case OPIOCGETCHILD:
482 if (copy_from_user(&node, argp, sizeof(int)))
483 return -EFAULT;
484
485 if (cmd == OPIOCGETNEXT)
486 node = __prom_getsibling(node);
487 else
488 node = __prom_getchild(node);
489
490 if (__copy_to_user(argp, &node, sizeof(int)))
491 return -EFAULT;
492
493 return 0;
494
495 default:
496 if (cnt++ < 10)
497 printk(KERN_INFO "openprom_bsd_ioctl: cmd 0x%X\n", cmd);
498 return -EINVAL;
499
500 }
501}
502
503
504/*
505 * Handoff control to the correct ioctl handler.
506 */
507static int openprom_ioctl(struct inode * inode, struct file * file,
508 unsigned int cmd, unsigned long arg)
509{
510 DATA *data = (DATA *) file->private_data;
511 static int cnt;
512
513 switch (cmd) {
514 case OPROMGETOPT:
515 case OPROMNXTOPT:
516 if ((file->f_mode & FMODE_READ) == 0)
517 return -EPERM;
518 return openprom_sunos_ioctl(inode, file, cmd, arg,
519 options_node);
520
521 case OPROMSETOPT:
522 case OPROMSETOPT2:
523 if ((file->f_mode & FMODE_WRITE) == 0)
524 return -EPERM;
525 return openprom_sunos_ioctl(inode, file, cmd, arg,
526 options_node);
527
528 case OPROMNEXT:
529 case OPROMCHILD:
530 case OPROMGETPROP:
531 case OPROMNXTPROP:
532 if ((file->f_mode & FMODE_READ) == 0)
533 return -EPERM;
534 return openprom_sunos_ioctl(inode, file, cmd, arg,
535 data->current_node);
536
537 case OPROMU2P:
538 case OPROMGETCONS:
539 case OPROMGETFBNAME:
540 case OPROMGETBOOTARGS:
541 case OPROMSETCUR:
542 case OPROMPCI2NODE:
543 case OPROMPATH2NODE:
544 if ((file->f_mode & FMODE_READ) == 0)
545 return -EPERM;
546 return openprom_sunos_ioctl(inode, file, cmd, arg, 0);
547
548 case OPIOCGET:
549 case OPIOCNEXTPROP:
550 case OPIOCGETOPTNODE:
551 case OPIOCGETNEXT:
552 case OPIOCGETCHILD:
553 if ((file->f_mode & FMODE_READ) == 0)
554 return -EBADF;
555 return openprom_bsd_ioctl(inode,file,cmd,arg);
556
557 case OPIOCSET:
558 if ((file->f_mode & FMODE_WRITE) == 0)
559 return -EBADF;
560 return openprom_bsd_ioctl(inode,file,cmd,arg);
561
562 default:
563 if (cnt++ < 10)
564 printk("openprom_ioctl: cmd 0x%X, arg 0x%lX\n", cmd, arg);
565 return -EINVAL;
566 }
567}
568
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800569static long openprom_compat_ioctl(struct file *file, unsigned int cmd,
570 unsigned long arg)
571{
572 long rval = -ENOTTY;
573
574 /*
575 * SunOS/Solaris only, the NetBSD one's have embedded pointers in
576 * the arg which we'd need to clean up...
577 */
578 switch (cmd) {
579 case OPROMGETOPT:
580 case OPROMSETOPT:
581 case OPROMNXTOPT:
582 case OPROMSETOPT2:
583 case OPROMNEXT:
584 case OPROMCHILD:
585 case OPROMGETPROP:
586 case OPROMNXTPROP:
587 case OPROMU2P:
588 case OPROMGETCONS:
589 case OPROMGETFBNAME:
590 case OPROMGETBOOTARGS:
591 case OPROMSETCUR:
592 case OPROMPCI2NODE:
593 case OPROMPATH2NODE:
594 lock_kernel();
595 rval = openprom_ioctl(file->f_dentry->d_inode, file, cmd, arg);
596 lock_kernel();
597 break;
598 }
David S. Millerd5a858b2005-11-08 10:00:13 -0800599
600 return rval;
Christoph Hellwigb31023f2005-11-07 14:12:47 -0800601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int openprom_open(struct inode * inode, struct file * file)
604{
605 DATA *data;
606
607 data = (DATA *) kmalloc(sizeof(DATA), GFP_KERNEL);
608 if (!data)
609 return -ENOMEM;
610
611 data->current_node = prom_root_node;
612 data->lastnode = prom_root_node;
613 file->private_data = (void *)data;
614
615 return 0;
616}
617
618static int openprom_release(struct inode * inode, struct file * file)
619{
620 kfree(file->private_data);
621 return 0;
622}
623
624static struct file_operations openprom_fops = {
625 .owner = THIS_MODULE,
626 .llseek = no_llseek,
627 .ioctl = openprom_ioctl,
David S. Millerd5a858b2005-11-08 10:00:13 -0800628 .compat_ioctl = openprom_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 .open = openprom_open,
630 .release = openprom_release,
631};
632
633static struct miscdevice openprom_dev = {
634 SUN_OPENPROM_MINOR, "openprom", &openprom_fops
635};
636
637static int __init openprom_init(void)
638{
639 int error;
640
641 error = misc_register(&openprom_dev);
642 if (error) {
643 printk(KERN_ERR "openprom: unable to get misc minor\n");
644 return error;
645 }
646
647 options_node = prom_getchild(prom_root_node);
648 options_node = prom_searchsiblings(options_node,"options");
649
650 if (options_node == 0 || options_node == -1) {
651 printk(KERN_ERR "openprom: unable to find options node\n");
652 misc_deregister(&openprom_dev);
653 return -EIO;
654 }
655
656 return 0;
657}
658
659static void __exit openprom_cleanup(void)
660{
661 misc_deregister(&openprom_dev);
662}
663
664module_init(openprom_init);
665module_exit(openprom_cleanup);
666MODULE_LICENSE("GPL");