procfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /* Sysctl interface for parport devices.
  2. *
  3. * Authors: David Campbell
  4. * Tim Waugh <tim@cyberelk.demon.co.uk>
  5. * Philip Blundell <philb@gnu.org>
  6. * Andrea Arcangeli
  7. * Riccardo Facchetti <fizban@tin.it>
  8. *
  9. * based on work by Grant Guenther <grant@torque.net>
  10. * and Philip Blundell
  11. *
  12. * Cleaned up include files - Russell King <linux@arm.uk.linux.org>
  13. */
  14. #include <linux/string.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/parport.h>
  21. #include <linux/ctype.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/device.h>
  24. #include <asm/uaccess.h>
  25. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  26. #define PARPORT_MIN_TIMESLICE_VALUE 1ul
  27. #define PARPORT_MAX_TIMESLICE_VALUE ((unsigned long) HZ)
  28. #define PARPORT_MIN_SPINTIME_VALUE 1
  29. #define PARPORT_MAX_SPINTIME_VALUE 1000
  30. static int do_active_device(struct ctl_table *table, int write,
  31. void __user *result, size_t *lenp, loff_t *ppos)
  32. {
  33. struct parport *port = (struct parport *)table->extra1;
  34. char buffer[256];
  35. struct pardevice *dev;
  36. int len = 0;
  37. if (write) /* can't happen anyway */
  38. return -EACCES;
  39. if (*ppos) {
  40. *lenp = 0;
  41. return 0;
  42. }
  43. for (dev = port->devices; dev ; dev = dev->next) {
  44. if(dev == port->cad) {
  45. len += sprintf(buffer, "%s\n", dev->name);
  46. }
  47. }
  48. if(!len) {
  49. len += sprintf(buffer, "%s\n", "none");
  50. }
  51. if (len > *lenp)
  52. len = *lenp;
  53. else
  54. *lenp = len;
  55. *ppos += len;
  56. return copy_to_user(result, buffer, len) ? -EFAULT : 0;
  57. }
  58. #ifdef CONFIG_PARPORT_1284
  59. static int do_autoprobe(struct ctl_table *table, int write,
  60. void __user *result, size_t *lenp, loff_t *ppos)
  61. {
  62. struct parport_device_info *info = table->extra2;
  63. const char *str;
  64. char buffer[256];
  65. int len = 0;
  66. if (write) /* permissions stop this */
  67. return -EACCES;
  68. if (*ppos) {
  69. *lenp = 0;
  70. return 0;
  71. }
  72. if ((str = info->class_name) != NULL)
  73. len += sprintf (buffer + len, "CLASS:%s;\n", str);
  74. if ((str = info->model) != NULL)
  75. len += sprintf (buffer + len, "MODEL:%s;\n", str);
  76. if ((str = info->mfr) != NULL)
  77. len += sprintf (buffer + len, "MANUFACTURER:%s;\n", str);
  78. if ((str = info->description) != NULL)
  79. len += sprintf (buffer + len, "DESCRIPTION:%s;\n", str);
  80. if ((str = info->cmdset) != NULL)
  81. len += sprintf (buffer + len, "COMMAND SET:%s;\n", str);
  82. if (len > *lenp)
  83. len = *lenp;
  84. else
  85. *lenp = len;
  86. *ppos += len;
  87. return copy_to_user (result, buffer, len) ? -EFAULT : 0;
  88. }
  89. #endif /* IEEE1284.3 support. */
  90. static int do_hardware_base_addr(struct ctl_table *table, int write,
  91. void __user *result,
  92. size_t *lenp, loff_t *ppos)
  93. {
  94. struct parport *port = (struct parport *)table->extra1;
  95. char buffer[20];
  96. int len = 0;
  97. if (*ppos) {
  98. *lenp = 0;
  99. return 0;
  100. }
  101. if (write) /* permissions prevent this anyway */
  102. return -EACCES;
  103. len += sprintf (buffer, "%lu\t%lu\n", port->base, port->base_hi);
  104. if (len > *lenp)
  105. len = *lenp;
  106. else
  107. *lenp = len;
  108. *ppos += len;
  109. return copy_to_user(result, buffer, len) ? -EFAULT : 0;
  110. }
  111. static int do_hardware_irq(struct ctl_table *table, int write,
  112. void __user *result,
  113. size_t *lenp, loff_t *ppos)
  114. {
  115. struct parport *port = (struct parport *)table->extra1;
  116. char buffer[20];
  117. int len = 0;
  118. if (*ppos) {
  119. *lenp = 0;
  120. return 0;
  121. }
  122. if (write) /* permissions prevent this anyway */
  123. return -EACCES;
  124. len += sprintf (buffer, "%d\n", port->irq);
  125. if (len > *lenp)
  126. len = *lenp;
  127. else
  128. *lenp = len;
  129. *ppos += len;
  130. return copy_to_user(result, buffer, len) ? -EFAULT : 0;
  131. }
  132. static int do_hardware_dma(struct ctl_table *table, int write,
  133. void __user *result,
  134. size_t *lenp, loff_t *ppos)
  135. {
  136. struct parport *port = (struct parport *)table->extra1;
  137. char buffer[20];
  138. int len = 0;
  139. if (*ppos) {
  140. *lenp = 0;
  141. return 0;
  142. }
  143. if (write) /* permissions prevent this anyway */
  144. return -EACCES;
  145. len += sprintf (buffer, "%d\n", port->dma);
  146. if (len > *lenp)
  147. len = *lenp;
  148. else
  149. *lenp = len;
  150. *ppos += len;
  151. return copy_to_user(result, buffer, len) ? -EFAULT : 0;
  152. }
  153. static int do_hardware_modes(struct ctl_table *table, int write,
  154. void __user *result,
  155. size_t *lenp, loff_t *ppos)
  156. {
  157. struct parport *port = (struct parport *)table->extra1;
  158. char buffer[40];
  159. int len = 0;
  160. if (*ppos) {
  161. *lenp = 0;
  162. return 0;
  163. }
  164. if (write) /* permissions prevent this anyway */
  165. return -EACCES;
  166. {
  167. #define printmode(x) {if(port->modes&PARPORT_MODE_##x){len+=sprintf(buffer+len,"%s%s",f?",":"",#x);f++;}}
  168. int f = 0;
  169. printmode(PCSPP);
  170. printmode(TRISTATE);
  171. printmode(COMPAT);
  172. printmode(EPP);
  173. printmode(ECP);
  174. printmode(DMA);
  175. #undef printmode
  176. }
  177. buffer[len++] = '\n';
  178. if (len > *lenp)
  179. len = *lenp;
  180. else
  181. *lenp = len;
  182. *ppos += len;
  183. return copy_to_user(result, buffer, len) ? -EFAULT : 0;
  184. }
  185. #define PARPORT_PORT_DIR(CHILD) { .procname = NULL, .mode = 0555, .child = CHILD }
  186. #define PARPORT_PARPORT_DIR(CHILD) { .procname = "parport", \
  187. .mode = 0555, .child = CHILD }
  188. #define PARPORT_DEV_DIR(CHILD) { .procname = "dev", .mode = 0555, .child = CHILD }
  189. #define PARPORT_DEVICES_ROOT_DIR { .procname = "devices", \
  190. .mode = 0555, .child = NULL }
  191. static const unsigned long parport_min_timeslice_value =
  192. PARPORT_MIN_TIMESLICE_VALUE;
  193. static const unsigned long parport_max_timeslice_value =
  194. PARPORT_MAX_TIMESLICE_VALUE;
  195. static const int parport_min_spintime_value =
  196. PARPORT_MIN_SPINTIME_VALUE;
  197. static const int parport_max_spintime_value =
  198. PARPORT_MAX_SPINTIME_VALUE;
  199. struct parport_sysctl_table {
  200. struct ctl_table_header *sysctl_header;
  201. struct ctl_table vars[12];
  202. struct ctl_table device_dir[2];
  203. struct ctl_table port_dir[2];
  204. struct ctl_table parport_dir[2];
  205. struct ctl_table dev_dir[2];
  206. };
  207. static const struct parport_sysctl_table parport_sysctl_template = {
  208. .sysctl_header = NULL,
  209. {
  210. {
  211. .procname = "spintime",
  212. .data = NULL,
  213. .maxlen = sizeof(int),
  214. .mode = 0644,
  215. .proc_handler = proc_dointvec_minmax,
  216. .extra1 = (void*) &parport_min_spintime_value,
  217. .extra2 = (void*) &parport_max_spintime_value
  218. },
  219. {
  220. .procname = "base-addr",
  221. .data = NULL,
  222. .maxlen = 0,
  223. .mode = 0444,
  224. .proc_handler = do_hardware_base_addr
  225. },
  226. {
  227. .procname = "irq",
  228. .data = NULL,
  229. .maxlen = 0,
  230. .mode = 0444,
  231. .proc_handler = do_hardware_irq
  232. },
  233. {
  234. .procname = "dma",
  235. .data = NULL,
  236. .maxlen = 0,
  237. .mode = 0444,
  238. .proc_handler = do_hardware_dma
  239. },
  240. {
  241. .procname = "modes",
  242. .data = NULL,
  243. .maxlen = 0,
  244. .mode = 0444,
  245. .proc_handler = do_hardware_modes
  246. },
  247. PARPORT_DEVICES_ROOT_DIR,
  248. #ifdef CONFIG_PARPORT_1284
  249. {
  250. .procname = "autoprobe",
  251. .data = NULL,
  252. .maxlen = 0,
  253. .mode = 0444,
  254. .proc_handler = do_autoprobe
  255. },
  256. {
  257. .procname = "autoprobe0",
  258. .data = NULL,
  259. .maxlen = 0,
  260. .mode = 0444,
  261. .proc_handler = do_autoprobe
  262. },
  263. {
  264. .procname = "autoprobe1",
  265. .data = NULL,
  266. .maxlen = 0,
  267. .mode = 0444,
  268. .proc_handler = do_autoprobe
  269. },
  270. {
  271. .procname = "autoprobe2",
  272. .data = NULL,
  273. .maxlen = 0,
  274. .mode = 0444,
  275. .proc_handler = do_autoprobe
  276. },
  277. {
  278. .procname = "autoprobe3",
  279. .data = NULL,
  280. .maxlen = 0,
  281. .mode = 0444,
  282. .proc_handler = do_autoprobe
  283. },
  284. #endif /* IEEE 1284 support */
  285. {}
  286. },
  287. {
  288. {
  289. .procname = "active",
  290. .data = NULL,
  291. .maxlen = 0,
  292. .mode = 0444,
  293. .proc_handler = do_active_device
  294. },
  295. {}
  296. },
  297. {
  298. PARPORT_PORT_DIR(NULL),
  299. {}
  300. },
  301. {
  302. PARPORT_PARPORT_DIR(NULL),
  303. {}
  304. },
  305. {
  306. PARPORT_DEV_DIR(NULL),
  307. {}
  308. }
  309. };
  310. struct parport_device_sysctl_table
  311. {
  312. struct ctl_table_header *sysctl_header;
  313. struct ctl_table vars[2];
  314. struct ctl_table device_dir[2];
  315. struct ctl_table devices_root_dir[2];
  316. struct ctl_table port_dir[2];
  317. struct ctl_table parport_dir[2];
  318. struct ctl_table dev_dir[2];
  319. };
  320. static const struct parport_device_sysctl_table
  321. parport_device_sysctl_template = {
  322. .sysctl_header = NULL,
  323. {
  324. {
  325. .procname = "timeslice",
  326. .data = NULL,
  327. .maxlen = sizeof(unsigned long),
  328. .mode = 0644,
  329. .proc_handler = proc_doulongvec_ms_jiffies_minmax,
  330. .extra1 = (void*) &parport_min_timeslice_value,
  331. .extra2 = (void*) &parport_max_timeslice_value
  332. },
  333. },
  334. {
  335. {
  336. .procname = NULL,
  337. .data = NULL,
  338. .maxlen = 0,
  339. .mode = 0555,
  340. .child = NULL
  341. },
  342. {}
  343. },
  344. {
  345. PARPORT_DEVICES_ROOT_DIR,
  346. {}
  347. },
  348. {
  349. PARPORT_PORT_DIR(NULL),
  350. {}
  351. },
  352. {
  353. PARPORT_PARPORT_DIR(NULL),
  354. {}
  355. },
  356. {
  357. PARPORT_DEV_DIR(NULL),
  358. {}
  359. }
  360. };
  361. struct parport_default_sysctl_table
  362. {
  363. struct ctl_table_header *sysctl_header;
  364. struct ctl_table vars[3];
  365. struct ctl_table default_dir[2];
  366. struct ctl_table parport_dir[2];
  367. struct ctl_table dev_dir[2];
  368. };
  369. static struct parport_default_sysctl_table
  370. parport_default_sysctl_table = {
  371. .sysctl_header = NULL,
  372. {
  373. {
  374. .procname = "timeslice",
  375. .data = &parport_default_timeslice,
  376. .maxlen = sizeof(parport_default_timeslice),
  377. .mode = 0644,
  378. .proc_handler = proc_doulongvec_ms_jiffies_minmax,
  379. .extra1 = (void*) &parport_min_timeslice_value,
  380. .extra2 = (void*) &parport_max_timeslice_value
  381. },
  382. {
  383. .procname = "spintime",
  384. .data = &parport_default_spintime,
  385. .maxlen = sizeof(parport_default_spintime),
  386. .mode = 0644,
  387. .proc_handler = proc_dointvec_minmax,
  388. .extra1 = (void*) &parport_min_spintime_value,
  389. .extra2 = (void*) &parport_max_spintime_value
  390. },
  391. {}
  392. },
  393. {
  394. {
  395. .procname = "default",
  396. .mode = 0555,
  397. .child = parport_default_sysctl_table.vars
  398. },
  399. {}
  400. },
  401. {
  402. PARPORT_PARPORT_DIR(parport_default_sysctl_table.default_dir),
  403. {}
  404. },
  405. {
  406. PARPORT_DEV_DIR(parport_default_sysctl_table.parport_dir),
  407. {}
  408. }
  409. };
  410. int parport_proc_register(struct parport *port)
  411. {
  412. struct parport_sysctl_table *t;
  413. int i;
  414. t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
  415. if (t == NULL)
  416. return -ENOMEM;
  417. t->device_dir[0].extra1 = port;
  418. for (i = 0; i < 5; i++)
  419. t->vars[i].extra1 = port;
  420. t->vars[0].data = &port->spintime;
  421. t->vars[5].child = t->device_dir;
  422. for (i = 0; i < 5; i++)
  423. t->vars[6 + i].extra2 = &port->probe_info[i];
  424. t->port_dir[0].procname = port->name;
  425. t->port_dir[0].child = t->vars;
  426. t->parport_dir[0].child = t->port_dir;
  427. t->dev_dir[0].child = t->parport_dir;
  428. t->sysctl_header = register_sysctl_table(t->dev_dir);
  429. if (t->sysctl_header == NULL) {
  430. kfree(t);
  431. t = NULL;
  432. }
  433. port->sysctl_table = t;
  434. return 0;
  435. }
  436. int parport_proc_unregister(struct parport *port)
  437. {
  438. if (port->sysctl_table) {
  439. struct parport_sysctl_table *t = port->sysctl_table;
  440. port->sysctl_table = NULL;
  441. unregister_sysctl_table(t->sysctl_header);
  442. kfree(t);
  443. }
  444. return 0;
  445. }
  446. int parport_device_proc_register(struct pardevice *device)
  447. {
  448. struct parport_device_sysctl_table *t;
  449. struct parport * port = device->port;
  450. t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
  451. if (t == NULL)
  452. return -ENOMEM;
  453. t->dev_dir[0].child = t->parport_dir;
  454. t->parport_dir[0].child = t->port_dir;
  455. t->port_dir[0].procname = port->name;
  456. t->port_dir[0].child = t->devices_root_dir;
  457. t->devices_root_dir[0].child = t->device_dir;
  458. t->device_dir[0].procname = device->name;
  459. t->device_dir[0].child = t->vars;
  460. t->vars[0].data = &device->timeslice;
  461. t->sysctl_header = register_sysctl_table(t->dev_dir);
  462. if (t->sysctl_header == NULL) {
  463. kfree(t);
  464. t = NULL;
  465. }
  466. device->sysctl_table = t;
  467. return 0;
  468. }
  469. int parport_device_proc_unregister(struct pardevice *device)
  470. {
  471. if (device->sysctl_table) {
  472. struct parport_device_sysctl_table *t = device->sysctl_table;
  473. device->sysctl_table = NULL;
  474. unregister_sysctl_table(t->sysctl_header);
  475. kfree(t);
  476. }
  477. return 0;
  478. }
  479. static int __init parport_default_proc_register(void)
  480. {
  481. int ret;
  482. parport_default_sysctl_table.sysctl_header =
  483. register_sysctl_table(parport_default_sysctl_table.dev_dir);
  484. if (!parport_default_sysctl_table.sysctl_header)
  485. return -ENOMEM;
  486. ret = parport_bus_init();
  487. if (ret) {
  488. unregister_sysctl_table(parport_default_sysctl_table.
  489. sysctl_header);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static void __exit parport_default_proc_unregister(void)
  495. {
  496. if (parport_default_sysctl_table.sysctl_header) {
  497. unregister_sysctl_table(parport_default_sysctl_table.
  498. sysctl_header);
  499. parport_default_sysctl_table.sysctl_header = NULL;
  500. }
  501. parport_bus_exit();
  502. }
  503. #else /* no sysctl or no procfs*/
  504. int parport_proc_register(struct parport *pp)
  505. {
  506. return 0;
  507. }
  508. int parport_proc_unregister(struct parport *pp)
  509. {
  510. return 0;
  511. }
  512. int parport_device_proc_register(struct pardevice *device)
  513. {
  514. return 0;
  515. }
  516. int parport_device_proc_unregister(struct pardevice *device)
  517. {
  518. return 0;
  519. }
  520. static int __init parport_default_proc_register (void)
  521. {
  522. return parport_bus_init();
  523. }
  524. static void __exit parport_default_proc_unregister (void)
  525. {
  526. parport_bus_exit();
  527. }
  528. #endif
  529. module_init(parport_default_proc_register)
  530. module_exit(parport_default_proc_unregister)