procfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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) { 0, NULL, NULL, 0, 0555, child }
  185. #define PARPORT_PARPORT_DIR(child) { DEV_PARPORT, "parport", \
  186. NULL, 0, 0555, child }
  187. #define PARPORT_DEV_DIR(child) { CTL_DEV, "dev", NULL, 0, 0555, child }
  188. #define PARPORT_DEVICES_ROOT_DIR { DEV_PARPORT_DEVICES, "devices", \
  189. NULL, 0, 0555, 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. NULL,
  208. {
  209. { DEV_PARPORT_SPINTIME, "spintime",
  210. NULL, sizeof(int), 0644, NULL,
  211. &proc_dointvec_minmax, NULL, NULL,
  212. (void*) &parport_min_spintime_value,
  213. (void*) &parport_max_spintime_value },
  214. { DEV_PARPORT_BASE_ADDR, "base-addr",
  215. NULL, 0, 0444, NULL,
  216. &do_hardware_base_addr },
  217. { DEV_PARPORT_IRQ, "irq",
  218. NULL, 0, 0444, NULL,
  219. &do_hardware_irq },
  220. { DEV_PARPORT_DMA, "dma",
  221. NULL, 0, 0444, NULL,
  222. &do_hardware_dma },
  223. { DEV_PARPORT_MODES, "modes",
  224. NULL, 0, 0444, NULL,
  225. &do_hardware_modes },
  226. PARPORT_DEVICES_ROOT_DIR,
  227. #ifdef CONFIG_PARPORT_1284
  228. { DEV_PARPORT_AUTOPROBE, "autoprobe",
  229. NULL, 0, 0444, NULL,
  230. &do_autoprobe },
  231. { DEV_PARPORT_AUTOPROBE + 1, "autoprobe0",
  232. NULL, 0, 0444, NULL,
  233. &do_autoprobe },
  234. { DEV_PARPORT_AUTOPROBE + 2, "autoprobe1",
  235. NULL, 0, 0444, NULL,
  236. &do_autoprobe },
  237. { DEV_PARPORT_AUTOPROBE + 3, "autoprobe2",
  238. NULL, 0, 0444, NULL,
  239. &do_autoprobe },
  240. { DEV_PARPORT_AUTOPROBE + 4, "autoprobe3",
  241. NULL, 0, 0444, NULL,
  242. &do_autoprobe },
  243. #endif /* IEEE 1284 support */
  244. {0}
  245. },
  246. { {DEV_PARPORT_DEVICES_ACTIVE, "active", NULL, 0, 0444, NULL,
  247. &do_active_device }, {0}},
  248. { PARPORT_PORT_DIR(NULL), {0}},
  249. { PARPORT_PARPORT_DIR(NULL), {0}},
  250. { PARPORT_DEV_DIR(NULL), {0}}
  251. };
  252. struct parport_device_sysctl_table
  253. {
  254. struct ctl_table_header *sysctl_header;
  255. ctl_table vars[2];
  256. ctl_table device_dir[2];
  257. ctl_table devices_root_dir[2];
  258. ctl_table port_dir[2];
  259. ctl_table parport_dir[2];
  260. ctl_table dev_dir[2];
  261. };
  262. static const struct parport_device_sysctl_table
  263. parport_device_sysctl_template = {
  264. NULL,
  265. {
  266. { DEV_PARPORT_DEVICE_TIMESLICE, "timeslice",
  267. NULL, sizeof(int), 0644, NULL,
  268. &proc_doulongvec_ms_jiffies_minmax, NULL, NULL,
  269. (void*) &parport_min_timeslice_value,
  270. (void*) &parport_max_timeslice_value },
  271. },
  272. { {0, NULL, NULL, 0, 0555, NULL}, {0}},
  273. { PARPORT_DEVICES_ROOT_DIR, {0}},
  274. { PARPORT_PORT_DIR(NULL), {0}},
  275. { PARPORT_PARPORT_DIR(NULL), {0}},
  276. { PARPORT_DEV_DIR(NULL), {0}}
  277. };
  278. struct parport_default_sysctl_table
  279. {
  280. struct ctl_table_header *sysctl_header;
  281. ctl_table vars[3];
  282. ctl_table default_dir[2];
  283. ctl_table parport_dir[2];
  284. ctl_table dev_dir[2];
  285. };
  286. extern unsigned long parport_default_timeslice;
  287. extern int parport_default_spintime;
  288. static struct parport_default_sysctl_table
  289. parport_default_sysctl_table = {
  290. NULL,
  291. {
  292. { DEV_PARPORT_DEFAULT_TIMESLICE, "timeslice",
  293. &parport_default_timeslice,
  294. sizeof(parport_default_timeslice), 0644, NULL,
  295. &proc_doulongvec_ms_jiffies_minmax, NULL, NULL,
  296. (void*) &parport_min_timeslice_value,
  297. (void*) &parport_max_timeslice_value },
  298. { DEV_PARPORT_DEFAULT_SPINTIME, "spintime",
  299. &parport_default_spintime,
  300. sizeof(parport_default_spintime), 0644, NULL,
  301. &proc_dointvec_minmax, NULL, NULL,
  302. (void*) &parport_min_spintime_value,
  303. (void*) &parport_max_spintime_value },
  304. {0}
  305. },
  306. { { DEV_PARPORT_DEFAULT, "default", NULL, 0, 0555,
  307. parport_default_sysctl_table.vars },{0}},
  308. {
  309. PARPORT_PARPORT_DIR(parport_default_sysctl_table.default_dir),
  310. {0}},
  311. { PARPORT_DEV_DIR(parport_default_sysctl_table.parport_dir), {0}}
  312. };
  313. int parport_proc_register(struct parport *port)
  314. {
  315. struct parport_sysctl_table *t;
  316. int i;
  317. t = kmalloc(sizeof(*t), GFP_KERNEL);
  318. if (t == NULL)
  319. return -ENOMEM;
  320. memcpy(t, &parport_sysctl_template, sizeof(*t));
  321. t->device_dir[0].extra1 = port;
  322. for (i = 0; i < 8; i++)
  323. t->vars[i].extra1 = port;
  324. t->vars[0].data = &port->spintime;
  325. t->vars[5].child = t->device_dir;
  326. for (i = 0; i < 5; i++)
  327. t->vars[6 + i].extra2 = &port->probe_info[i];
  328. t->port_dir[0].procname = port->name;
  329. t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
  330. t->port_dir[0].child = t->vars;
  331. t->parport_dir[0].child = t->port_dir;
  332. t->dev_dir[0].child = t->parport_dir;
  333. t->sysctl_header = register_sysctl_table(t->dev_dir, 0);
  334. if (t->sysctl_header == NULL) {
  335. kfree(t);
  336. t = NULL;
  337. }
  338. port->sysctl_table = t;
  339. return 0;
  340. }
  341. int parport_proc_unregister(struct parport *port)
  342. {
  343. if (port->sysctl_table) {
  344. struct parport_sysctl_table *t = port->sysctl_table;
  345. port->sysctl_table = NULL;
  346. unregister_sysctl_table(t->sysctl_header);
  347. kfree(t);
  348. }
  349. return 0;
  350. }
  351. int parport_device_proc_register(struct pardevice *device)
  352. {
  353. struct parport_device_sysctl_table *t;
  354. struct parport * port = device->port;
  355. t = kmalloc(sizeof(*t), GFP_KERNEL);
  356. if (t == NULL)
  357. return -ENOMEM;
  358. memcpy(t, &parport_device_sysctl_template, sizeof(*t));
  359. t->dev_dir[0].child = t->parport_dir;
  360. t->parport_dir[0].child = t->port_dir;
  361. t->port_dir[0].procname = port->name;
  362. t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
  363. t->port_dir[0].child = t->devices_root_dir;
  364. t->devices_root_dir[0].child = t->device_dir;
  365. #ifdef CONFIG_PARPORT_1284
  366. t->device_dir[0].ctl_name =
  367. parport_device_num(port->number, port->muxport,
  368. device->daisy)
  369. + 1; /* nb 0 isn't legal here */
  370. #else /* No IEEE 1284 support */
  371. /* parport_device_num isn't available. */
  372. t->device_dir[0].ctl_name = 1;
  373. #endif /* IEEE 1284 support or not */
  374. t->device_dir[0].procname = device->name;
  375. t->device_dir[0].extra1 = device;
  376. t->device_dir[0].child = t->vars;
  377. t->vars[0].data = &device->timeslice;
  378. t->sysctl_header = register_sysctl_table(t->dev_dir, 0);
  379. if (t->sysctl_header == NULL) {
  380. kfree(t);
  381. t = NULL;
  382. }
  383. device->sysctl_table = t;
  384. return 0;
  385. }
  386. int parport_device_proc_unregister(struct pardevice *device)
  387. {
  388. if (device->sysctl_table) {
  389. struct parport_device_sysctl_table *t = device->sysctl_table;
  390. device->sysctl_table = NULL;
  391. unregister_sysctl_table(t->sysctl_header);
  392. kfree(t);
  393. }
  394. return 0;
  395. }
  396. static int __init parport_default_proc_register(void)
  397. {
  398. parport_default_sysctl_table.sysctl_header =
  399. register_sysctl_table(parport_default_sysctl_table.dev_dir, 0);
  400. return 0;
  401. }
  402. static void __exit parport_default_proc_unregister(void)
  403. {
  404. if (parport_default_sysctl_table.sysctl_header) {
  405. unregister_sysctl_table(parport_default_sysctl_table.
  406. sysctl_header);
  407. parport_default_sysctl_table.sysctl_header = NULL;
  408. }
  409. }
  410. #else /* no sysctl or no procfs*/
  411. int parport_proc_register(struct parport *pp)
  412. {
  413. return 0;
  414. }
  415. int parport_proc_unregister(struct parport *pp)
  416. {
  417. return 0;
  418. }
  419. int parport_device_proc_register(struct pardevice *device)
  420. {
  421. return 0;
  422. }
  423. int parport_device_proc_unregister(struct pardevice *device)
  424. {
  425. return 0;
  426. }
  427. static int __init parport_default_proc_register (void)
  428. {
  429. return 0;
  430. }
  431. static void __exit parport_default_proc_unregister (void)
  432. {
  433. }
  434. #endif
  435. module_init(parport_default_proc_register)
  436. module_exit(parport_default_proc_unregister)