ide-proc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 1997-1998 Mark Lord
  4. * Copyright (C) 2003 Red Hat
  5. *
  6. * Some code was moved here from ide.c, see it for original copyrights.
  7. */
  8. /*
  9. * This is the /proc/ide/ filesystem implementation.
  10. *
  11. * Drive/Driver settings can be retrieved by reading the drive's
  12. * "settings" files. e.g. "cat /proc/ide0/hda/settings"
  13. * To write a new value "val" into a specific setting "name", use:
  14. * echo "name:val" >/proc/ide/ide0/hda/settings
  15. */
  16. #include <linux/module.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/errno.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/stat.h>
  21. #include <linux/mm.h>
  22. #include <linux/pci.h>
  23. #include <linux/ctype.h>
  24. #include <linux/ide.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/slab.h>
  27. #include <asm/io.h>
  28. static struct proc_dir_entry *proc_ide_root;
  29. static int ide_imodel_proc_show(struct seq_file *m, void *v)
  30. {
  31. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  32. const char *name;
  33. switch (hwif->chipset) {
  34. case ide_generic: name = "generic"; break;
  35. case ide_pci: name = "pci"; break;
  36. case ide_cmd640: name = "cmd640"; break;
  37. case ide_dtc2278: name = "dtc2278"; break;
  38. case ide_ali14xx: name = "ali14xx"; break;
  39. case ide_qd65xx: name = "qd65xx"; break;
  40. case ide_umc8672: name = "umc8672"; break;
  41. case ide_ht6560b: name = "ht6560b"; break;
  42. case ide_4drives: name = "4drives"; break;
  43. case ide_pmac: name = "mac-io"; break;
  44. case ide_au1xxx: name = "au1xxx"; break;
  45. case ide_palm3710: name = "palm3710"; break;
  46. case ide_acorn: name = "acorn"; break;
  47. default: name = "(unknown)"; break;
  48. }
  49. seq_printf(m, "%s\n", name);
  50. return 0;
  51. }
  52. static int ide_mate_proc_show(struct seq_file *m, void *v)
  53. {
  54. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  55. if (hwif && hwif->mate)
  56. seq_printf(m, "%s\n", hwif->mate->name);
  57. else
  58. seq_printf(m, "(none)\n");
  59. return 0;
  60. }
  61. static int ide_channel_proc_show(struct seq_file *m, void *v)
  62. {
  63. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  64. seq_printf(m, "%c\n", hwif->channel ? '1' : '0');
  65. return 0;
  66. }
  67. static int ide_identify_proc_show(struct seq_file *m, void *v)
  68. {
  69. ide_drive_t *drive = (ide_drive_t *)m->private;
  70. u8 *buf;
  71. if (!drive) {
  72. seq_putc(m, '\n');
  73. return 0;
  74. }
  75. buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  76. if (!buf)
  77. return -ENOMEM;
  78. if (taskfile_lib_get_identify(drive, buf) == 0) {
  79. __le16 *val = (__le16 *)buf;
  80. int i;
  81. for (i = 0; i < SECTOR_SIZE / 2; i++) {
  82. seq_printf(m, "%04x%c", le16_to_cpu(val[i]),
  83. (i % 8) == 7 ? '\n' : ' ');
  84. }
  85. } else
  86. seq_putc(m, buf[0]);
  87. kfree(buf);
  88. return 0;
  89. }
  90. /**
  91. * ide_find_setting - find a specific setting
  92. * @st: setting table pointer
  93. * @name: setting name
  94. *
  95. * Scan's the setting table for a matching entry and returns
  96. * this or NULL if no entry is found. The caller must hold the
  97. * setting semaphore
  98. */
  99. static
  100. const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
  101. char *name)
  102. {
  103. while (st->name) {
  104. if (strcmp(st->name, name) == 0)
  105. break;
  106. st++;
  107. }
  108. return st->name ? st : NULL;
  109. }
  110. /**
  111. * ide_read_setting - read an IDE setting
  112. * @drive: drive to read from
  113. * @setting: drive setting
  114. *
  115. * Read a drive setting and return the value. The caller
  116. * must hold the ide_setting_mtx when making this call.
  117. *
  118. * BUGS: the data return and error are the same return value
  119. * so an error -EINVAL and true return of the same value cannot
  120. * be told apart
  121. */
  122. static int ide_read_setting(ide_drive_t *drive,
  123. const struct ide_proc_devset *setting)
  124. {
  125. const struct ide_devset *ds = setting->setting;
  126. int val = -EINVAL;
  127. if (ds->get)
  128. val = ds->get(drive);
  129. return val;
  130. }
  131. /**
  132. * ide_write_setting - read an IDE setting
  133. * @drive: drive to read from
  134. * @setting: drive setting
  135. * @val: value
  136. *
  137. * Write a drive setting if it is possible. The caller
  138. * must hold the ide_setting_mtx when making this call.
  139. *
  140. * BUGS: the data return and error are the same return value
  141. * so an error -EINVAL and true return of the same value cannot
  142. * be told apart
  143. *
  144. * FIXME: This should be changed to enqueue a special request
  145. * to the driver to change settings, and then wait on a sema for completion.
  146. * The current scheme of polling is kludgy, though safe enough.
  147. */
  148. static int ide_write_setting(ide_drive_t *drive,
  149. const struct ide_proc_devset *setting, int val)
  150. {
  151. const struct ide_devset *ds = setting->setting;
  152. if (!capable(CAP_SYS_ADMIN))
  153. return -EACCES;
  154. if (!ds->set)
  155. return -EPERM;
  156. if ((ds->flags & DS_SYNC)
  157. && (val < setting->min || val > setting->max))
  158. return -EINVAL;
  159. return ide_devset_execute(drive, ds, val);
  160. }
  161. ide_devset_get(xfer_rate, current_speed);
  162. static int set_xfer_rate (ide_drive_t *drive, int arg)
  163. {
  164. struct ide_cmd cmd;
  165. if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
  166. return -EINVAL;
  167. memset(&cmd, 0, sizeof(cmd));
  168. cmd.tf.command = ATA_CMD_SET_FEATURES;
  169. cmd.tf.feature = SETFEATURES_XFER;
  170. cmd.tf.nsect = (u8)arg;
  171. cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
  172. cmd.valid.in.tf = IDE_VALID_NSECT;
  173. cmd.tf_flags = IDE_TFLAG_SET_XFER;
  174. return ide_no_data_taskfile(drive, &cmd);
  175. }
  176. ide_devset_rw(current_speed, xfer_rate);
  177. ide_devset_rw_field(init_speed, init_speed);
  178. ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
  179. ide_devset_ro_field(number, dn);
  180. static const struct ide_proc_devset ide_generic_settings[] = {
  181. IDE_PROC_DEVSET(current_speed, 0, 70),
  182. IDE_PROC_DEVSET(init_speed, 0, 70),
  183. IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
  184. IDE_PROC_DEVSET(keepsettings, 0, 1),
  185. IDE_PROC_DEVSET(nice1, 0, 1),
  186. IDE_PROC_DEVSET(number, 0, 3),
  187. IDE_PROC_DEVSET(pio_mode, 0, 255),
  188. IDE_PROC_DEVSET(unmaskirq, 0, 1),
  189. IDE_PROC_DEVSET(using_dma, 0, 1),
  190. { NULL },
  191. };
  192. static void proc_ide_settings_warn(void)
  193. {
  194. printk_once(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
  195. "obsolete, and will be removed soon!\n");
  196. }
  197. static int ide_settings_proc_show(struct seq_file *m, void *v)
  198. {
  199. const struct ide_proc_devset *setting, *g, *d;
  200. const struct ide_devset *ds;
  201. ide_drive_t *drive = (ide_drive_t *) m->private;
  202. int rc, mul_factor, div_factor;
  203. proc_ide_settings_warn();
  204. mutex_lock(&ide_setting_mtx);
  205. g = ide_generic_settings;
  206. d = drive->settings;
  207. seq_printf(m, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
  208. seq_printf(m, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
  209. while (g->name || (d && d->name)) {
  210. /* read settings in the alphabetical order */
  211. if (g->name && d && d->name) {
  212. if (strcmp(d->name, g->name) < 0)
  213. setting = d++;
  214. else
  215. setting = g++;
  216. } else if (d && d->name) {
  217. setting = d++;
  218. } else
  219. setting = g++;
  220. mul_factor = setting->mulf ? setting->mulf(drive) : 1;
  221. div_factor = setting->divf ? setting->divf(drive) : 1;
  222. seq_printf(m, "%-24s", setting->name);
  223. rc = ide_read_setting(drive, setting);
  224. if (rc >= 0)
  225. seq_printf(m, "%-16d", rc * mul_factor / div_factor);
  226. else
  227. seq_printf(m, "%-16s", "write-only");
  228. seq_printf(m, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
  229. ds = setting->setting;
  230. if (ds->get)
  231. seq_printf(m, "r");
  232. if (ds->set)
  233. seq_printf(m, "w");
  234. seq_printf(m, "\n");
  235. }
  236. mutex_unlock(&ide_setting_mtx);
  237. return 0;
  238. }
  239. static int ide_settings_proc_open(struct inode *inode, struct file *file)
  240. {
  241. return single_open(file, ide_settings_proc_show, PDE_DATA(inode));
  242. }
  243. #define MAX_LEN 30
  244. static ssize_t ide_settings_proc_write(struct file *file, const char __user *buffer,
  245. size_t count, loff_t *pos)
  246. {
  247. ide_drive_t *drive = PDE_DATA(file_inode(file));
  248. char name[MAX_LEN + 1];
  249. int for_real = 0, mul_factor, div_factor;
  250. unsigned long n;
  251. const struct ide_proc_devset *setting;
  252. char *buf, *s;
  253. if (!capable(CAP_SYS_ADMIN))
  254. return -EACCES;
  255. proc_ide_settings_warn();
  256. if (count >= PAGE_SIZE)
  257. return -EINVAL;
  258. s = buf = (char *)__get_free_page(GFP_USER);
  259. if (!buf)
  260. return -ENOMEM;
  261. if (copy_from_user(buf, buffer, count)) {
  262. free_page((unsigned long)buf);
  263. return -EFAULT;
  264. }
  265. buf[count] = '\0';
  266. /*
  267. * Skip over leading whitespace
  268. */
  269. while (count && isspace(*s)) {
  270. --count;
  271. ++s;
  272. }
  273. /*
  274. * Do one full pass to verify all parameters,
  275. * then do another to actually write the new settings.
  276. */
  277. do {
  278. char *p = s;
  279. n = count;
  280. while (n > 0) {
  281. unsigned val;
  282. char *q = p;
  283. while (n > 0 && *p != ':') {
  284. --n;
  285. p++;
  286. }
  287. if (*p != ':')
  288. goto parse_error;
  289. if (p - q > MAX_LEN)
  290. goto parse_error;
  291. memcpy(name, q, p - q);
  292. name[p - q] = 0;
  293. if (n > 0) {
  294. --n;
  295. p++;
  296. } else
  297. goto parse_error;
  298. val = simple_strtoul(p, &q, 10);
  299. n -= q - p;
  300. p = q;
  301. if (n > 0 && !isspace(*p))
  302. goto parse_error;
  303. while (n > 0 && isspace(*p)) {
  304. --n;
  305. ++p;
  306. }
  307. mutex_lock(&ide_setting_mtx);
  308. /* generic settings first, then driver specific ones */
  309. setting = ide_find_setting(ide_generic_settings, name);
  310. if (!setting) {
  311. if (drive->settings)
  312. setting = ide_find_setting(drive->settings, name);
  313. if (!setting) {
  314. mutex_unlock(&ide_setting_mtx);
  315. goto parse_error;
  316. }
  317. }
  318. if (for_real) {
  319. mul_factor = setting->mulf ? setting->mulf(drive) : 1;
  320. div_factor = setting->divf ? setting->divf(drive) : 1;
  321. ide_write_setting(drive, setting, val * div_factor / mul_factor);
  322. }
  323. mutex_unlock(&ide_setting_mtx);
  324. }
  325. } while (!for_real++);
  326. free_page((unsigned long)buf);
  327. return count;
  328. parse_error:
  329. free_page((unsigned long)buf);
  330. printk("%s(): parse error\n", __func__);
  331. return -EINVAL;
  332. }
  333. static const struct proc_ops ide_settings_proc_ops = {
  334. .proc_open = ide_settings_proc_open,
  335. .proc_read = seq_read,
  336. .proc_lseek = seq_lseek,
  337. .proc_release = single_release,
  338. .proc_write = ide_settings_proc_write,
  339. };
  340. int ide_capacity_proc_show(struct seq_file *m, void *v)
  341. {
  342. seq_printf(m, "%llu\n", (long long)0x7fffffff);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(ide_capacity_proc_show);
  346. int ide_geometry_proc_show(struct seq_file *m, void *v)
  347. {
  348. ide_drive_t *drive = (ide_drive_t *) m->private;
  349. seq_printf(m, "physical %d/%d/%d\n",
  350. drive->cyl, drive->head, drive->sect);
  351. seq_printf(m, "logical %d/%d/%d\n",
  352. drive->bios_cyl, drive->bios_head, drive->bios_sect);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(ide_geometry_proc_show);
  356. static int ide_dmodel_proc_show(struct seq_file *seq, void *v)
  357. {
  358. ide_drive_t *drive = (ide_drive_t *) seq->private;
  359. char *m = (char *)&drive->id[ATA_ID_PROD];
  360. seq_printf(seq, "%.40s\n", m[0] ? m : "(none)");
  361. return 0;
  362. }
  363. static int ide_driver_proc_show(struct seq_file *m, void *v)
  364. {
  365. ide_drive_t *drive = (ide_drive_t *)m->private;
  366. struct device *dev = &drive->gendev;
  367. struct ide_driver *ide_drv;
  368. if (dev->driver) {
  369. ide_drv = to_ide_driver(dev->driver);
  370. seq_printf(m, "%s version %s\n",
  371. dev->driver->name, ide_drv->version);
  372. } else
  373. seq_printf(m, "ide-default version 0.9.newide\n");
  374. return 0;
  375. }
  376. static int ide_media_proc_show(struct seq_file *m, void *v)
  377. {
  378. ide_drive_t *drive = (ide_drive_t *) m->private;
  379. const char *media;
  380. switch (drive->media) {
  381. case ide_disk: media = "disk\n"; break;
  382. case ide_cdrom: media = "cdrom\n"; break;
  383. case ide_tape: media = "tape\n"; break;
  384. case ide_floppy: media = "floppy\n"; break;
  385. case ide_optical: media = "optical\n"; break;
  386. default: media = "UNKNOWN\n"; break;
  387. }
  388. seq_puts(m, media);
  389. return 0;
  390. }
  391. static int ide_media_proc_open(struct inode *inode, struct file *file)
  392. {
  393. return single_open(file, ide_media_proc_show, PDE_DATA(inode));
  394. }
  395. static const struct file_operations ide_media_proc_fops = {
  396. .owner = THIS_MODULE,
  397. .open = ide_media_proc_open,
  398. .read = seq_read,
  399. .llseek = seq_lseek,
  400. .release = single_release,
  401. };
  402. static ide_proc_entry_t generic_drive_entries[] = {
  403. { "driver", S_IFREG|S_IRUGO, ide_driver_proc_show },
  404. { "identify", S_IFREG|S_IRUSR, ide_identify_proc_show },
  405. { "media", S_IFREG|S_IRUGO, ide_media_proc_show },
  406. { "model", S_IFREG|S_IRUGO, ide_dmodel_proc_show },
  407. {}
  408. };
  409. static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
  410. {
  411. struct proc_dir_entry *ent;
  412. if (!dir || !p)
  413. return;
  414. while (p->name != NULL) {
  415. ent = proc_create_single_data(p->name, p->mode, dir, p->show, data);
  416. if (!ent) return;
  417. p++;
  418. }
  419. }
  420. static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
  421. {
  422. if (!dir || !p)
  423. return;
  424. while (p->name != NULL) {
  425. remove_proc_entry(p->name, dir);
  426. p++;
  427. }
  428. }
  429. void ide_proc_register_driver(ide_drive_t *drive, struct ide_driver *driver)
  430. {
  431. mutex_lock(&ide_setting_mtx);
  432. drive->settings = driver->proc_devsets(drive);
  433. mutex_unlock(&ide_setting_mtx);
  434. ide_add_proc_entries(drive->proc, driver->proc_entries(drive), drive);
  435. }
  436. EXPORT_SYMBOL(ide_proc_register_driver);
  437. /**
  438. * ide_proc_unregister_driver - remove driver specific data
  439. * @drive: drive
  440. * @driver: driver
  441. *
  442. * Clean up the driver specific /proc files and IDE settings
  443. * for a given drive.
  444. *
  445. * Takes ide_setting_mtx.
  446. */
  447. void ide_proc_unregister_driver(ide_drive_t *drive, struct ide_driver *driver)
  448. {
  449. ide_remove_proc_entries(drive->proc, driver->proc_entries(drive));
  450. mutex_lock(&ide_setting_mtx);
  451. /*
  452. * ide_setting_mtx protects both the settings list and the use
  453. * of settings (we cannot take a setting out that is being used).
  454. */
  455. drive->settings = NULL;
  456. mutex_unlock(&ide_setting_mtx);
  457. }
  458. EXPORT_SYMBOL(ide_proc_unregister_driver);
  459. void ide_proc_port_register_devices(ide_hwif_t *hwif)
  460. {
  461. struct proc_dir_entry *ent;
  462. struct proc_dir_entry *parent = hwif->proc;
  463. ide_drive_t *drive;
  464. char name[64];
  465. int i;
  466. ide_port_for_each_dev(i, drive, hwif) {
  467. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  468. continue;
  469. drive->proc = proc_mkdir(drive->name, parent);
  470. if (drive->proc) {
  471. ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
  472. proc_create_data("settings", S_IFREG|S_IRUSR|S_IWUSR,
  473. drive->proc, &ide_settings_proc_ops,
  474. drive);
  475. }
  476. sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
  477. ent = proc_symlink(drive->name, proc_ide_root, name);
  478. if (!ent) return;
  479. }
  480. }
  481. void ide_proc_unregister_device(ide_drive_t *drive)
  482. {
  483. if (drive->proc) {
  484. remove_proc_entry("settings", drive->proc);
  485. ide_remove_proc_entries(drive->proc, generic_drive_entries);
  486. remove_proc_entry(drive->name, proc_ide_root);
  487. remove_proc_entry(drive->name, drive->hwif->proc);
  488. drive->proc = NULL;
  489. }
  490. }
  491. static ide_proc_entry_t hwif_entries[] = {
  492. { "channel", S_IFREG|S_IRUGO, ide_channel_proc_show },
  493. { "mate", S_IFREG|S_IRUGO, ide_mate_proc_show },
  494. { "model", S_IFREG|S_IRUGO, ide_imodel_proc_show },
  495. {}
  496. };
  497. void ide_proc_register_port(ide_hwif_t *hwif)
  498. {
  499. if (!hwif->proc) {
  500. hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
  501. if (!hwif->proc)
  502. return;
  503. ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
  504. }
  505. }
  506. void ide_proc_unregister_port(ide_hwif_t *hwif)
  507. {
  508. if (hwif->proc) {
  509. ide_remove_proc_entries(hwif->proc, hwif_entries);
  510. remove_proc_entry(hwif->name, proc_ide_root);
  511. hwif->proc = NULL;
  512. }
  513. }
  514. static int proc_print_driver(struct device_driver *drv, void *data)
  515. {
  516. struct ide_driver *ide_drv = to_ide_driver(drv);
  517. struct seq_file *s = data;
  518. seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
  519. return 0;
  520. }
  521. static int ide_drivers_show(struct seq_file *s, void *p)
  522. {
  523. int err;
  524. err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
  525. if (err < 0)
  526. printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
  527. __func__, err);
  528. return 0;
  529. }
  530. DEFINE_PROC_SHOW_ATTRIBUTE(ide_drivers);
  531. void proc_ide_create(void)
  532. {
  533. proc_ide_root = proc_mkdir("ide", NULL);
  534. if (!proc_ide_root)
  535. return;
  536. proc_create("drivers", 0, proc_ide_root, &ide_drivers_proc_ops);
  537. }
  538. void proc_ide_destroy(void)
  539. {
  540. remove_proc_entry("drivers", proc_ide_root);
  541. remove_proc_entry("ide", NULL);
  542. }