openprom.c 16 KB

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