procfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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, struct file *filp,
  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, struct file *filp,
  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. struct file *filp, 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. struct file *filp, 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. struct file *filp, 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. struct file *filp, 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) { .ctl_name = 0, .procname = NULL, .mode = 0555, .child = CHILD }
  185. #define PARPORT_PARPORT_DIR(CHILD) { .ctl_name = DEV_PARPORT, .procname = "parport", \
  186. .mode = 0555, .child = CHILD }
  187. #define PARPORT_DEV_DIR(CHILD) { .ctl_name = CTL_DEV, .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. .ctl_name = 0,
  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. ctl_table vars[3];
  365. ctl_table default_dir[2];
  366. ctl_table parport_dir[2];
  367. 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. .ctl_name = DEV_PARPORT_DEFAULT,
  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 = kmalloc(sizeof(*t), GFP_KERNEL);
  416. if (t == NULL)
  417. return -ENOMEM;
  418. memcpy(t, &parport_sysctl_template, sizeof(*t));
  419. t->device_dir[0].extra1 = port;
  420. for (i = 0; i < 5; i++)
  421. t->vars[i].extra1 = port;
  422. t->vars[0].data = &port->spintime;
  423. t->vars[5].child = t->device_dir;
  424. for (i = 0; i < 5; i++)
  425. t->vars[6 + i].extra2 = &port->probe_info[i];
  426. t->port_dir[0].procname = port->name;
  427. t->port_dir[0].ctl_name = 0;
  428. t->port_dir[0].child = t->vars;
  429. t->parport_dir[0].child = t->port_dir;
  430. t->dev_dir[0].child = t->parport_dir;
  431. t->sysctl_header = register_sysctl_table(t->dev_dir);
  432. if (t->sysctl_header == NULL) {
  433. kfree(t);
  434. t = NULL;
  435. }
  436. port->sysctl_table = t;
  437. return 0;
  438. }
  439. int parport_proc_unregister(struct parport *port)
  440. {
  441. if (port->sysctl_table) {
  442. struct parport_sysctl_table *t = port->sysctl_table;
  443. port->sysctl_table = NULL;
  444. unregister_sysctl_table(t->sysctl_header);
  445. kfree(t);
  446. }
  447. return 0;
  448. }
  449. int parport_device_proc_register(struct pardevice *device)
  450. {
  451. struct parport_device_sysctl_table *t;
  452. struct parport * port = device->port;
  453. t = kmalloc(sizeof(*t), GFP_KERNEL);
  454. if (t == NULL)
  455. return -ENOMEM;
  456. memcpy(t, &parport_device_sysctl_template, sizeof(*t));
  457. t->dev_dir[0].child = t->parport_dir;
  458. t->parport_dir[0].child = t->port_dir;
  459. t->port_dir[0].procname = port->name;
  460. t->port_dir[0].ctl_name = 0;
  461. t->port_dir[0].child = t->devices_root_dir;
  462. t->devices_root_dir[0].child = t->device_dir;
  463. t->device_dir[0].ctl_name = 0;
  464. t->device_dir[0].procname = device->name;
  465. t->device_dir[0].child = t->vars;
  466. t->vars[0].data = &device->timeslice;
  467. t->sysctl_header = register_sysctl_table(t->dev_dir);
  468. if (t->sysctl_header == NULL) {
  469. kfree(t);
  470. t = NULL;
  471. }
  472. device->sysctl_table = t;
  473. return 0;
  474. }
  475. int parport_device_proc_unregister(struct pardevice *device)
  476. {
  477. if (device->sysctl_table) {
  478. struct parport_device_sysctl_table *t = device->sysctl_table;
  479. device->sysctl_table = NULL;
  480. unregister_sysctl_table(t->sysctl_header);
  481. kfree(t);
  482. }
  483. return 0;
  484. }
  485. static int __init parport_default_proc_register(void)
  486. {
  487. parport_default_sysctl_table.sysctl_header =
  488. register_sysctl_table(parport_default_sysctl_table.dev_dir);
  489. return 0;
  490. }
  491. static void __exit parport_default_proc_unregister(void)
  492. {
  493. if (parport_default_sysctl_table.sysctl_header) {
  494. unregister_sysctl_table(parport_default_sysctl_table.
  495. sysctl_header);
  496. parport_default_sysctl_table.sysctl_header = NULL;
  497. }
  498. }
  499. #else /* no sysctl or no procfs*/
  500. int parport_proc_register(struct parport *pp)
  501. {
  502. return 0;
  503. }
  504. int parport_proc_unregister(struct parport *pp)
  505. {
  506. return 0;
  507. }
  508. int parport_device_proc_register(struct pardevice *device)
  509. {
  510. return 0;
  511. }
  512. int parport_device_proc_unregister(struct pardevice *device)
  513. {
  514. return 0;
  515. }
  516. static int __init parport_default_proc_register (void)
  517. {
  518. return 0;
  519. }
  520. static void __exit parport_default_proc_unregister (void)
  521. {
  522. }
  523. #endif
  524. module_init(parport_default_proc_register)
  525. module_exit(parport_default_proc_unregister)