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