procfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 { .ctl_name = DEV_PARPORT_DEVICES, .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. .ctl_name = DEV_PARPORT_SPINTIME,
  211. .procname = "spintime",
  212. .data = NULL,
  213. .maxlen = sizeof(int),
  214. .mode = 0644,
  215. .proc_handler = &proc_dointvec_minmax,
  216. .extra1 = (void*) &parport_min_spintime_value,
  217. .extra2 = (void*) &parport_max_spintime_value
  218. },
  219. {
  220. .ctl_name = DEV_PARPORT_BASE_ADDR,
  221. .procname = "base-addr",
  222. .data = NULL,
  223. .maxlen = 0,
  224. .mode = 0444,
  225. .proc_handler = &do_hardware_base_addr
  226. },
  227. {
  228. .ctl_name = DEV_PARPORT_IRQ,
  229. .procname = "irq",
  230. .data = NULL,
  231. .maxlen = 0,
  232. .mode = 0444,
  233. .proc_handler = &do_hardware_irq
  234. },
  235. {
  236. .ctl_name = DEV_PARPORT_DMA,
  237. .procname = "dma",
  238. .data = NULL,
  239. .maxlen = 0,
  240. .mode = 0444,
  241. .proc_handler = &do_hardware_dma
  242. },
  243. {
  244. .ctl_name = DEV_PARPORT_MODES,
  245. .procname = "modes",
  246. .data = NULL,
  247. .maxlen = 0,
  248. .mode = 0444,
  249. .proc_handler = &do_hardware_modes
  250. },
  251. PARPORT_DEVICES_ROOT_DIR,
  252. #ifdef CONFIG_PARPORT_1284
  253. {
  254. .ctl_name = DEV_PARPORT_AUTOPROBE,
  255. .procname = "autoprobe",
  256. .data = NULL,
  257. .maxlen = 0,
  258. .mode = 0444,
  259. .proc_handler = &do_autoprobe
  260. },
  261. {
  262. .ctl_name = DEV_PARPORT_AUTOPROBE + 1,
  263. .procname = "autoprobe0",
  264. .data = NULL,
  265. .maxlen = 0,
  266. .mode = 0444,
  267. .proc_handler = &do_autoprobe
  268. },
  269. {
  270. .ctl_name = DEV_PARPORT_AUTOPROBE + 2,
  271. .procname = "autoprobe1",
  272. .data = NULL,
  273. .maxlen = 0,
  274. .mode = 0444,
  275. .proc_handler = &do_autoprobe
  276. },
  277. {
  278. .ctl_name = DEV_PARPORT_AUTOPROBE + 3,
  279. .procname = "autoprobe2",
  280. .data = NULL,
  281. .maxlen = 0,
  282. .mode = 0444,
  283. .proc_handler = &do_autoprobe
  284. },
  285. {
  286. .ctl_name = DEV_PARPORT_AUTOPROBE + 4,
  287. .procname = "autoprobe3",
  288. .data = NULL,
  289. .maxlen = 0,
  290. .mode = 0444,
  291. .proc_handler = &do_autoprobe
  292. },
  293. #endif /* IEEE 1284 support */
  294. {}
  295. },
  296. {
  297. {
  298. .ctl_name = DEV_PARPORT_DEVICES_ACTIVE,
  299. .procname = "active",
  300. .data = NULL,
  301. .maxlen = 0,
  302. .mode = 0444,
  303. .proc_handler = &do_active_device
  304. },
  305. {}
  306. },
  307. {
  308. PARPORT_PORT_DIR(NULL),
  309. {}
  310. },
  311. {
  312. PARPORT_PARPORT_DIR(NULL),
  313. {}
  314. },
  315. {
  316. PARPORT_DEV_DIR(NULL),
  317. {}
  318. }
  319. };
  320. struct parport_device_sysctl_table
  321. {
  322. struct ctl_table_header *sysctl_header;
  323. ctl_table vars[2];
  324. ctl_table device_dir[2];
  325. ctl_table devices_root_dir[2];
  326. ctl_table port_dir[2];
  327. ctl_table parport_dir[2];
  328. ctl_table dev_dir[2];
  329. };
  330. static const struct parport_device_sysctl_table
  331. parport_device_sysctl_template = {
  332. .sysctl_header = NULL,
  333. {
  334. {
  335. .ctl_name = DEV_PARPORT_DEVICE_TIMESLICE,
  336. .procname = "timeslice",
  337. .data = NULL,
  338. .maxlen = sizeof(int),
  339. .mode = 0644,
  340. .proc_handler = &proc_doulongvec_ms_jiffies_minmax,
  341. .extra1 = (void*) &parport_min_timeslice_value,
  342. .extra2 = (void*) &parport_max_timeslice_value
  343. },
  344. },
  345. {
  346. {
  347. .ctl_name = 0,
  348. .procname = NULL,
  349. .data = NULL,
  350. .maxlen = 0,
  351. .mode = 0555,
  352. .child = NULL
  353. },
  354. {}
  355. },
  356. {
  357. PARPORT_DEVICES_ROOT_DIR,
  358. {}
  359. },
  360. {
  361. PARPORT_PORT_DIR(NULL),
  362. {}
  363. },
  364. {
  365. PARPORT_PARPORT_DIR(NULL),
  366. {}
  367. },
  368. {
  369. PARPORT_DEV_DIR(NULL),
  370. {}
  371. }
  372. };
  373. struct parport_default_sysctl_table
  374. {
  375. struct ctl_table_header *sysctl_header;
  376. ctl_table vars[3];
  377. ctl_table default_dir[2];
  378. ctl_table parport_dir[2];
  379. ctl_table dev_dir[2];
  380. };
  381. extern unsigned long parport_default_timeslice;
  382. extern int parport_default_spintime;
  383. static struct parport_default_sysctl_table
  384. parport_default_sysctl_table = {
  385. .sysctl_header = NULL,
  386. {
  387. {
  388. .ctl_name = DEV_PARPORT_DEFAULT_TIMESLICE,
  389. .procname = "timeslice",
  390. .data = &parport_default_timeslice,
  391. .maxlen = sizeof(parport_default_timeslice),
  392. .mode = 0644,
  393. .proc_handler = &proc_doulongvec_ms_jiffies_minmax,
  394. .extra1 = (void*) &parport_min_timeslice_value,
  395. .extra2 = (void*) &parport_max_timeslice_value
  396. },
  397. {
  398. .ctl_name = DEV_PARPORT_DEFAULT_SPINTIME,
  399. .procname = "spintime",
  400. .data = &parport_default_spintime,
  401. .maxlen = sizeof(parport_default_spintime),
  402. .mode = 0644,
  403. .proc_handler = &proc_dointvec_minmax,
  404. .extra1 = (void*) &parport_min_spintime_value,
  405. .extra2 = (void*) &parport_max_spintime_value
  406. },
  407. {}
  408. },
  409. {
  410. {
  411. .ctl_name = DEV_PARPORT_DEFAULT,
  412. .procname = "default",
  413. .mode = 0555,
  414. .child = parport_default_sysctl_table.vars
  415. },
  416. {}
  417. },
  418. {
  419. PARPORT_PARPORT_DIR(parport_default_sysctl_table.default_dir),
  420. {}
  421. },
  422. {
  423. PARPORT_DEV_DIR(parport_default_sysctl_table.parport_dir),
  424. {}
  425. }
  426. };
  427. int parport_proc_register(struct parport *port)
  428. {
  429. struct parport_sysctl_table *t;
  430. int i;
  431. t = kmalloc(sizeof(*t), GFP_KERNEL);
  432. if (t == NULL)
  433. return -ENOMEM;
  434. memcpy(t, &parport_sysctl_template, sizeof(*t));
  435. t->device_dir[0].extra1 = port;
  436. for (i = 0; i < 8; i++)
  437. t->vars[i].extra1 = port;
  438. t->vars[0].data = &port->spintime;
  439. t->vars[5].child = t->device_dir;
  440. for (i = 0; i < 5; i++)
  441. t->vars[6 + i].extra2 = &port->probe_info[i];
  442. t->port_dir[0].procname = port->name;
  443. t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
  444. t->port_dir[0].child = t->vars;
  445. t->parport_dir[0].child = t->port_dir;
  446. t->dev_dir[0].child = t->parport_dir;
  447. t->sysctl_header = register_sysctl_table(t->dev_dir);
  448. if (t->sysctl_header == NULL) {
  449. kfree(t);
  450. t = NULL;
  451. }
  452. port->sysctl_table = t;
  453. return 0;
  454. }
  455. int parport_proc_unregister(struct parport *port)
  456. {
  457. if (port->sysctl_table) {
  458. struct parport_sysctl_table *t = port->sysctl_table;
  459. port->sysctl_table = NULL;
  460. unregister_sysctl_table(t->sysctl_header);
  461. kfree(t);
  462. }
  463. return 0;
  464. }
  465. int parport_device_proc_register(struct pardevice *device)
  466. {
  467. struct parport_device_sysctl_table *t;
  468. struct parport * port = device->port;
  469. t = kmalloc(sizeof(*t), GFP_KERNEL);
  470. if (t == NULL)
  471. return -ENOMEM;
  472. memcpy(t, &parport_device_sysctl_template, sizeof(*t));
  473. t->dev_dir[0].child = t->parport_dir;
  474. t->parport_dir[0].child = t->port_dir;
  475. t->port_dir[0].procname = port->name;
  476. t->port_dir[0].ctl_name = port->number + 1; /* nb 0 isn't legal here */
  477. t->port_dir[0].child = t->devices_root_dir;
  478. t->devices_root_dir[0].child = t->device_dir;
  479. #ifdef CONFIG_PARPORT_1284
  480. t->device_dir[0].ctl_name =
  481. parport_device_num(port->number, port->muxport,
  482. device->daisy)
  483. + 1; /* nb 0 isn't legal here */
  484. #else /* No IEEE 1284 support */
  485. /* parport_device_num isn't available. */
  486. t->device_dir[0].ctl_name = 1;
  487. #endif /* IEEE 1284 support or not */
  488. t->device_dir[0].procname = device->name;
  489. t->device_dir[0].extra1 = device;
  490. t->device_dir[0].child = t->vars;
  491. t->vars[0].data = &device->timeslice;
  492. t->sysctl_header = register_sysctl_table(t->dev_dir);
  493. if (t->sysctl_header == NULL) {
  494. kfree(t);
  495. t = NULL;
  496. }
  497. device->sysctl_table = t;
  498. return 0;
  499. }
  500. int parport_device_proc_unregister(struct pardevice *device)
  501. {
  502. if (device->sysctl_table) {
  503. struct parport_device_sysctl_table *t = device->sysctl_table;
  504. device->sysctl_table = NULL;
  505. unregister_sysctl_table(t->sysctl_header);
  506. kfree(t);
  507. }
  508. return 0;
  509. }
  510. static int __init parport_default_proc_register(void)
  511. {
  512. parport_default_sysctl_table.sysctl_header =
  513. register_sysctl_table(parport_default_sysctl_table.dev_dir);
  514. return 0;
  515. }
  516. static void __exit parport_default_proc_unregister(void)
  517. {
  518. if (parport_default_sysctl_table.sysctl_header) {
  519. unregister_sysctl_table(parport_default_sysctl_table.
  520. sysctl_header);
  521. parport_default_sysctl_table.sysctl_header = NULL;
  522. }
  523. }
  524. #else /* no sysctl or no procfs*/
  525. int parport_proc_register(struct parport *pp)
  526. {
  527. return 0;
  528. }
  529. int parport_proc_unregister(struct parport *pp)
  530. {
  531. return 0;
  532. }
  533. int parport_device_proc_register(struct pardevice *device)
  534. {
  535. return 0;
  536. }
  537. int parport_device_proc_unregister(struct pardevice *device)
  538. {
  539. return 0;
  540. }
  541. static int __init parport_default_proc_register (void)
  542. {
  543. return 0;
  544. }
  545. static void __exit parport_default_proc_unregister (void)
  546. {
  547. }
  548. #endif
  549. module_init(parport_default_proc_register)
  550. module_exit(parport_default_proc_unregister)