procfs.c 12 KB

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