procfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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(const 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 += scnprintf(buffer, sizeof(buffer), "%s\n", dev->name);
  47. }
  48. }
  49. if(!len) {
  50. len += scnprintf(buffer, sizeof(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(const 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 += scnprintf (buffer + len, sizeof(buffer) - len, "CLASS:%s;\n", str);
  76. if ((str = info->model) != NULL)
  77. len += scnprintf (buffer + len, sizeof(buffer) - len, "MODEL:%s;\n", str);
  78. if ((str = info->mfr) != NULL)
  79. len += scnprintf (buffer + len, sizeof(buffer) - len, "MANUFACTURER:%s;\n", str);
  80. if ((str = info->description) != NULL)
  81. len += scnprintf (buffer + len, sizeof(buffer) - len, "DESCRIPTION:%s;\n", str);
  82. if ((str = info->cmdset) != NULL)
  83. len += scnprintf (buffer + len, sizeof(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(const 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[64];
  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 += scnprintf (buffer, sizeof(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(const 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 += scnprintf (buffer, sizeof(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(const 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 += scnprintf (buffer, sizeof(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(const 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 += scnprintf(buffer + len, sizeof(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. 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 *port_header;
  202. struct ctl_table_header *devices_header;
  203. #ifdef CONFIG_PARPORT_1284
  204. struct ctl_table vars[10];
  205. #else
  206. struct ctl_table vars[5];
  207. #endif /* IEEE 1284 support */
  208. struct ctl_table device_dir[1];
  209. };
  210. static const struct parport_sysctl_table parport_sysctl_template = {
  211. .port_header = NULL,
  212. .devices_header = NULL,
  213. {
  214. {
  215. .procname = "spintime",
  216. .data = NULL,
  217. .maxlen = sizeof(int),
  218. .mode = 0644,
  219. .proc_handler = proc_dointvec_minmax,
  220. .extra1 = (void*) &parport_min_spintime_value,
  221. .extra2 = (void*) &parport_max_spintime_value
  222. },
  223. {
  224. .procname = "base-addr",
  225. .data = NULL,
  226. .maxlen = 0,
  227. .mode = 0444,
  228. .proc_handler = do_hardware_base_addr
  229. },
  230. {
  231. .procname = "irq",
  232. .data = NULL,
  233. .maxlen = 0,
  234. .mode = 0444,
  235. .proc_handler = do_hardware_irq
  236. },
  237. {
  238. .procname = "dma",
  239. .data = NULL,
  240. .maxlen = 0,
  241. .mode = 0444,
  242. .proc_handler = do_hardware_dma
  243. },
  244. {
  245. .procname = "modes",
  246. .data = NULL,
  247. .maxlen = 0,
  248. .mode = 0444,
  249. .proc_handler = do_hardware_modes
  250. },
  251. #ifdef CONFIG_PARPORT_1284
  252. {
  253. .procname = "autoprobe",
  254. .data = NULL,
  255. .maxlen = 0,
  256. .mode = 0444,
  257. .proc_handler = do_autoprobe
  258. },
  259. {
  260. .procname = "autoprobe0",
  261. .data = NULL,
  262. .maxlen = 0,
  263. .mode = 0444,
  264. .proc_handler = do_autoprobe
  265. },
  266. {
  267. .procname = "autoprobe1",
  268. .data = NULL,
  269. .maxlen = 0,
  270. .mode = 0444,
  271. .proc_handler = do_autoprobe
  272. },
  273. {
  274. .procname = "autoprobe2",
  275. .data = NULL,
  276. .maxlen = 0,
  277. .mode = 0444,
  278. .proc_handler = do_autoprobe
  279. },
  280. {
  281. .procname = "autoprobe3",
  282. .data = NULL,
  283. .maxlen = 0,
  284. .mode = 0444,
  285. .proc_handler = do_autoprobe
  286. },
  287. #endif /* IEEE 1284 support */
  288. },
  289. {
  290. {
  291. .procname = "active",
  292. .data = NULL,
  293. .maxlen = 0,
  294. .mode = 0444,
  295. .proc_handler = do_active_device
  296. },
  297. },
  298. };
  299. struct parport_device_sysctl_table
  300. {
  301. struct ctl_table_header *sysctl_header;
  302. struct ctl_table vars[1];
  303. struct ctl_table device_dir[1];
  304. };
  305. static const struct parport_device_sysctl_table
  306. parport_device_sysctl_template = {
  307. .sysctl_header = NULL,
  308. {
  309. {
  310. .procname = "timeslice",
  311. .data = NULL,
  312. .maxlen = sizeof(unsigned long),
  313. .mode = 0644,
  314. .proc_handler = proc_doulongvec_ms_jiffies_minmax,
  315. .extra1 = (void*) &parport_min_timeslice_value,
  316. .extra2 = (void*) &parport_max_timeslice_value
  317. },
  318. },
  319. {
  320. {
  321. .procname = NULL,
  322. .data = NULL,
  323. .maxlen = 0,
  324. .mode = 0555,
  325. },
  326. }
  327. };
  328. struct parport_default_sysctl_table
  329. {
  330. struct ctl_table_header *sysctl_header;
  331. struct ctl_table vars[2];
  332. };
  333. static struct parport_default_sysctl_table
  334. parport_default_sysctl_table = {
  335. .sysctl_header = NULL,
  336. {
  337. {
  338. .procname = "timeslice",
  339. .data = &parport_default_timeslice,
  340. .maxlen = sizeof(parport_default_timeslice),
  341. .mode = 0644,
  342. .proc_handler = proc_doulongvec_ms_jiffies_minmax,
  343. .extra1 = (void*) &parport_min_timeslice_value,
  344. .extra2 = (void*) &parport_max_timeslice_value
  345. },
  346. {
  347. .procname = "spintime",
  348. .data = &parport_default_spintime,
  349. .maxlen = sizeof(parport_default_spintime),
  350. .mode = 0644,
  351. .proc_handler = proc_dointvec_minmax,
  352. .extra1 = (void*) &parport_min_spintime_value,
  353. .extra2 = (void*) &parport_max_spintime_value
  354. },
  355. }
  356. };
  357. int parport_proc_register(struct parport *port)
  358. {
  359. struct parport_sysctl_table *t;
  360. char *tmp_dir_path;
  361. int i, err = 0;
  362. t = kmemdup(&parport_sysctl_template, sizeof(*t), GFP_KERNEL);
  363. if (t == NULL)
  364. return -ENOMEM;
  365. t->device_dir[0].extra1 = port;
  366. t->vars[0].data = &port->spintime;
  367. for (i = 0; i < 5; i++) {
  368. t->vars[i].extra1 = port;
  369. #ifdef CONFIG_PARPORT_1284
  370. t->vars[5 + i].extra2 = &port->probe_info[i];
  371. #endif /* IEEE 1284 support */
  372. }
  373. tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices", port->name);
  374. if (!tmp_dir_path) {
  375. err = -ENOMEM;
  376. goto exit_free_t;
  377. }
  378. t->devices_header = register_sysctl(tmp_dir_path, t->device_dir);
  379. if (t->devices_header == NULL) {
  380. err = -ENOENT;
  381. goto exit_free_tmp_dir_path;
  382. }
  383. kfree(tmp_dir_path);
  384. tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s", port->name);
  385. if (!tmp_dir_path) {
  386. err = -ENOMEM;
  387. goto unregister_devices_h;
  388. }
  389. t->port_header = register_sysctl(tmp_dir_path, t->vars);
  390. if (t->port_header == NULL) {
  391. err = -ENOENT;
  392. goto unregister_devices_h;
  393. }
  394. port->sysctl_table = t;
  395. kfree(tmp_dir_path);
  396. return 0;
  397. unregister_devices_h:
  398. unregister_sysctl_table(t->devices_header);
  399. exit_free_tmp_dir_path:
  400. kfree(tmp_dir_path);
  401. exit_free_t:
  402. kfree(t);
  403. return err;
  404. }
  405. int parport_proc_unregister(struct parport *port)
  406. {
  407. if (port->sysctl_table) {
  408. struct parport_sysctl_table *t = port->sysctl_table;
  409. port->sysctl_table = NULL;
  410. unregister_sysctl_table(t->devices_header);
  411. unregister_sysctl_table(t->port_header);
  412. kfree(t);
  413. }
  414. return 0;
  415. }
  416. int parport_device_proc_register(struct pardevice *device)
  417. {
  418. struct parport_device_sysctl_table *t;
  419. struct parport * port = device->port;
  420. char *tmp_dir_path;
  421. int err = 0;
  422. t = kmemdup(&parport_device_sysctl_template, sizeof(*t), GFP_KERNEL);
  423. if (t == NULL)
  424. return -ENOMEM;
  425. /* Allocate a buffer for two paths: dev/parport/PORT/devices/DEVICE. */
  426. tmp_dir_path = kasprintf(GFP_KERNEL, "dev/parport/%s/devices/%s", port->name, device->name);
  427. if (!tmp_dir_path) {
  428. err = -ENOMEM;
  429. goto exit_free_t;
  430. }
  431. t->vars[0].data = &device->timeslice;
  432. t->sysctl_header = register_sysctl(tmp_dir_path, t->vars);
  433. if (t->sysctl_header == NULL) {
  434. kfree(t);
  435. t = NULL;
  436. }
  437. device->sysctl_table = t;
  438. kfree(tmp_dir_path);
  439. return 0;
  440. exit_free_t:
  441. kfree(t);
  442. return err;
  443. }
  444. int parport_device_proc_unregister(struct pardevice *device)
  445. {
  446. if (device->sysctl_table) {
  447. struct parport_device_sysctl_table *t = device->sysctl_table;
  448. device->sysctl_table = NULL;
  449. unregister_sysctl_table(t->sysctl_header);
  450. kfree(t);
  451. }
  452. return 0;
  453. }
  454. static int __init parport_default_proc_register(void)
  455. {
  456. int ret;
  457. parport_default_sysctl_table.sysctl_header =
  458. register_sysctl("dev/parport/default", parport_default_sysctl_table.vars);
  459. if (!parport_default_sysctl_table.sysctl_header)
  460. return -ENOMEM;
  461. ret = parport_bus_init();
  462. if (ret) {
  463. unregister_sysctl_table(parport_default_sysctl_table.
  464. sysctl_header);
  465. return ret;
  466. }
  467. return 0;
  468. }
  469. static void __exit parport_default_proc_unregister(void)
  470. {
  471. if (parport_default_sysctl_table.sysctl_header) {
  472. unregister_sysctl_table(parport_default_sysctl_table.
  473. sysctl_header);
  474. parport_default_sysctl_table.sysctl_header = NULL;
  475. }
  476. parport_bus_exit();
  477. }
  478. #else /* no sysctl or no procfs*/
  479. int parport_proc_register(struct parport *pp)
  480. {
  481. return 0;
  482. }
  483. int parport_proc_unregister(struct parport *pp)
  484. {
  485. return 0;
  486. }
  487. int parport_device_proc_register(struct pardevice *device)
  488. {
  489. return 0;
  490. }
  491. int parport_device_proc_unregister(struct pardevice *device)
  492. {
  493. return 0;
  494. }
  495. static int __init parport_default_proc_register (void)
  496. {
  497. return parport_bus_init();
  498. }
  499. static void __exit parport_default_proc_unregister (void)
  500. {
  501. parport_bus_exit();
  502. }
  503. #endif
  504. subsys_initcall(parport_default_proc_register)
  505. module_exit(parport_default_proc_unregister)