procfs.c 13 KB

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