procfs.c 12 KB

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