ide-proc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (C) 1997-1998 Mark Lord
  3. * Copyright (C) 2003 Red Hat
  4. *
  5. * Some code was moved here from ide.c, see it for original copyrights.
  6. */
  7. /*
  8. * This is the /proc/ide/ filesystem implementation.
  9. *
  10. * Drive/Driver settings can be retrieved by reading the drive's
  11. * "settings" files. e.g. "cat /proc/ide0/hda/settings"
  12. * To write a new value "val" into a specific setting "name", use:
  13. * echo "name:val" >/proc/ide/ide0/hda/settings
  14. */
  15. #include <linux/module.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/errno.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/stat.h>
  20. #include <linux/mm.h>
  21. #include <linux/pci.h>
  22. #include <linux/ctype.h>
  23. #include <linux/ide.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/slab.h>
  26. #include <asm/io.h>
  27. static struct proc_dir_entry *proc_ide_root;
  28. static int ide_imodel_proc_show(struct seq_file *m, void *v)
  29. {
  30. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  31. const char *name;
  32. switch (hwif->chipset) {
  33. case ide_generic: name = "generic"; break;
  34. case ide_pci: name = "pci"; break;
  35. case ide_cmd640: name = "cmd640"; break;
  36. case ide_dtc2278: name = "dtc2278"; break;
  37. case ide_ali14xx: name = "ali14xx"; break;
  38. case ide_qd65xx: name = "qd65xx"; break;
  39. case ide_umc8672: name = "umc8672"; break;
  40. case ide_ht6560b: name = "ht6560b"; break;
  41. case ide_4drives: name = "4drives"; break;
  42. case ide_pmac: name = "mac-io"; break;
  43. case ide_au1xxx: name = "au1xxx"; break;
  44. case ide_palm3710: name = "palm3710"; break;
  45. case ide_acorn: name = "acorn"; break;
  46. default: name = "(unknown)"; break;
  47. }
  48. seq_printf(m, "%s\n", name);
  49. return 0;
  50. }
  51. static int ide_mate_proc_show(struct seq_file *m, void *v)
  52. {
  53. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  54. if (hwif && hwif->mate)
  55. seq_printf(m, "%s\n", hwif->mate->name);
  56. else
  57. seq_printf(m, "(none)\n");
  58. return 0;
  59. }
  60. static int ide_channel_proc_show(struct seq_file *m, void *v)
  61. {
  62. ide_hwif_t *hwif = (ide_hwif_t *) m->private;
  63. seq_printf(m, "%c\n", hwif->channel ? '1' : '0');
  64. return 0;
  65. }
  66. static int ide_identify_proc_show(struct seq_file *m, void *v)
  67. {
  68. ide_drive_t *drive = (ide_drive_t *)m->private;
  69. u8 *buf;
  70. if (!drive) {
  71. seq_putc(m, '\n');
  72. return 0;
  73. }
  74. buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  75. if (!buf)
  76. return -ENOMEM;
  77. if (taskfile_lib_get_identify(drive, buf) == 0) {
  78. __le16 *val = (__le16 *)buf;
  79. int i;
  80. for (i = 0; i < SECTOR_SIZE / 2; i++) {
  81. seq_printf(m, "%04x%c", le16_to_cpu(val[i]),
  82. (i % 8) == 7 ? '\n' : ' ');
  83. }
  84. } else
  85. seq_putc(m, buf[0]);
  86. kfree(buf);
  87. return 0;
  88. }
  89. /**
  90. * ide_find_setting - find a specific setting
  91. * @st: setting table pointer
  92. * @name: setting name
  93. *
  94. * Scan's the setting table for a matching entry and returns
  95. * this or NULL if no entry is found. The caller must hold the
  96. * setting semaphore
  97. */
  98. static
  99. const struct ide_proc_devset *ide_find_setting(const struct ide_proc_devset *st,
  100. char *name)
  101. {
  102. while (st->name) {
  103. if (strcmp(st->name, name) == 0)
  104. break;
  105. st++;
  106. }
  107. return st->name ? st : NULL;
  108. }
  109. /**
  110. * ide_read_setting - read an IDE setting
  111. * @drive: drive to read from
  112. * @setting: drive setting
  113. *
  114. * Read a drive setting and return the value. The caller
  115. * must hold the ide_setting_mtx when making this call.
  116. *
  117. * BUGS: the data return and error are the same return value
  118. * so an error -EINVAL and true return of the same value cannot
  119. * be told apart
  120. */
  121. static int ide_read_setting(ide_drive_t *drive,
  122. const struct ide_proc_devset *setting)
  123. {
  124. const struct ide_devset *ds = setting->setting;
  125. int val = -EINVAL;
  126. if (ds->get)
  127. val = ds->get(drive);
  128. return val;
  129. }
  130. /**
  131. * ide_write_setting - read an IDE setting
  132. * @drive: drive to read from
  133. * @setting: drive setting
  134. * @val: value
  135. *
  136. * Write a drive setting if it is possible. The caller
  137. * must hold the ide_setting_mtx when making this call.
  138. *
  139. * BUGS: the data return and error are the same return value
  140. * so an error -EINVAL and true return of the same value cannot
  141. * be told apart
  142. *
  143. * FIXME: This should be changed to enqueue a special request
  144. * to the driver to change settings, and then wait on a sema for completion.
  145. * The current scheme of polling is kludgy, though safe enough.
  146. */
  147. static int ide_write_setting(ide_drive_t *drive,
  148. const struct ide_proc_devset *setting, int val)
  149. {
  150. const struct ide_devset *ds = setting->setting;
  151. if (!capable(CAP_SYS_ADMIN))
  152. return -EACCES;
  153. if (!ds->set)
  154. return -EPERM;
  155. if ((ds->flags & DS_SYNC)
  156. && (val < setting->min || val > setting->max))
  157. return -EINVAL;
  158. return ide_devset_execute(drive, ds, val);
  159. }
  160. ide_devset_get(xfer_rate, current_speed);
  161. static int set_xfer_rate (ide_drive_t *drive, int arg)
  162. {
  163. struct ide_cmd cmd;
  164. if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
  165. return -EINVAL;
  166. memset(&cmd, 0, sizeof(cmd));
  167. cmd.tf.command = ATA_CMD_SET_FEATURES;
  168. cmd.tf.feature = SETFEATURES_XFER;
  169. cmd.tf.nsect = (u8)arg;
  170. cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
  171. cmd.valid.in.tf = IDE_VALID_NSECT;
  172. cmd.tf_flags = IDE_TFLAG_SET_XFER;
  173. return ide_no_data_taskfile(drive, &cmd);
  174. }
  175. ide_devset_rw(current_speed, xfer_rate);
  176. ide_devset_rw_field(init_speed, init_speed);
  177. ide_devset_rw_flag(nice1, IDE_DFLAG_NICE1);
  178. ide_devset_rw_field(number, dn);
  179. static const struct ide_proc_devset ide_generic_settings[] = {
  180. IDE_PROC_DEVSET(current_speed, 0, 70),
  181. IDE_PROC_DEVSET(init_speed, 0, 70),
  182. IDE_PROC_DEVSET(io_32bit, 0, 1 + (SUPPORT_VLB_SYNC << 1)),
  183. IDE_PROC_DEVSET(keepsettings, 0, 1),
  184. IDE_PROC_DEVSET(nice1, 0, 1),
  185. IDE_PROC_DEVSET(number, 0, 3),
  186. IDE_PROC_DEVSET(pio_mode, 0, 255),
  187. IDE_PROC_DEVSET(unmaskirq, 0, 1),
  188. IDE_PROC_DEVSET(using_dma, 0, 1),
  189. { NULL },
  190. };
  191. static void proc_ide_settings_warn(void)
  192. {
  193. printk_once(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
  194. "obsolete, and will be removed soon!\n");
  195. }
  196. static int ide_settings_proc_show(struct seq_file *m, void *v)
  197. {
  198. const struct ide_proc_devset *setting, *g, *d;
  199. const struct ide_devset *ds;
  200. ide_drive_t *drive = (ide_drive_t *) m->private;
  201. int rc, mul_factor, div_factor;
  202. proc_ide_settings_warn();
  203. mutex_lock(&ide_setting_mtx);
  204. g = ide_generic_settings;
  205. d = drive->settings;
  206. seq_printf(m, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
  207. seq_printf(m, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
  208. while (g->name || (d && d->name)) {
  209. /* read settings in the alphabetical order */
  210. if (g->name && d && d->name) {
  211. if (strcmp(d->name, g->name) < 0)
  212. setting = d++;
  213. else
  214. setting = g++;
  215. } else if (d && d->name) {
  216. setting = d++;
  217. } else
  218. setting = g++;
  219. mul_factor = setting->mulf ? setting->mulf(drive) : 1;
  220. div_factor = setting->divf ? setting->divf(drive) : 1;
  221. seq_printf(m, "%-24s", setting->name);
  222. rc = ide_read_setting(drive, setting);
  223. if (rc >= 0)
  224. seq_printf(m, "%-16d", rc * mul_factor / div_factor);
  225. else
  226. seq_printf(m, "%-16s", "write-only");
  227. seq_printf(m, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
  228. ds = setting->setting;
  229. if (ds->get)
  230. seq_printf(m, "r");
  231. if (ds->set)
  232. seq_printf(m, "w");
  233. seq_printf(m, "\n");
  234. }
  235. mutex_unlock(&ide_setting_mtx);
  236. return 0;
  237. }
  238. static int ide_settings_proc_open(struct inode *inode, struct file *file)
  239. {
  240. return single_open(file, ide_settings_proc_show, PDE_DATA(inode));
  241. }
  242. #define MAX_LEN 30
  243. static ssize_t ide_settings_proc_write(struct file *file, const char __user *buffer,
  244. size_t count, loff_t *pos)
  245. {
  246. ide_drive_t *drive = PDE_DATA(file_inode(file));
  247. char name[MAX_LEN + 1];
  248. int for_real = 0, mul_factor, div_factor;
  249. unsigned long n;
  250. const struct ide_proc_devset *setting;
  251. char *buf, *s;
  252. if (!capable(CAP_SYS_ADMIN))
  253. return -EACCES;
  254. proc_ide_settings_warn();
  255. if (count >= PAGE_SIZE)
  256. return -EINVAL;
  257. s = buf = (char *)__get_free_page(GFP_USER);
  258. if (!buf)
  259. return -ENOMEM;
  260. if (copy_from_user(buf, buffer, count)) {
  261. free_page((unsigned long)buf);
  262. return -EFAULT;
  263. }
  264. buf[count] = '\0';
  265. /*
  266. * Skip over leading whitespace
  267. */
  268. while (count && isspace(*s)) {
  269. --count;
  270. ++s;
  271. }
  272. /*
  273. * Do one full pass to verify all parameters,
  274. * then do another to actually write the new settings.
  275. */
  276. do {
  277. char *p = s;
  278. n = count;
  279. while (n > 0) {
  280. unsigned val;
  281. char *q = p;
  282. while (n > 0 && *p != ':') {
  283. --n;
  284. p++;
  285. }
  286. if (*p != ':')
  287. goto parse_error;
  288. if (p - q > MAX_LEN)
  289. goto parse_error;
  290. memcpy(name, q, p - q);
  291. name[p - q] = 0;
  292. if (n > 0) {
  293. --n;
  294. p++;
  295. } else
  296. goto parse_error;
  297. val = simple_strtoul(p, &q, 10);
  298. n -= q - p;
  299. p = q;
  300. if (n > 0 && !isspace(*p))
  301. goto parse_error;
  302. while (n > 0 && isspace(*p)) {
  303. --n;
  304. ++p;
  305. }
  306. mutex_lock(&ide_setting_mtx);
  307. /* generic settings first, then driver specific ones */
  308. setting = ide_find_setting(ide_generic_settings, name);
  309. if (!setting) {
  310. if (drive->settings)
  311. setting = ide_find_setting(drive->settings, name);
  312. if (!setting) {
  313. mutex_unlock(&ide_setting_mtx);
  314. goto parse_error;
  315. }
  316. }
  317. if (for_real) {
  318. mul_factor = setting->mulf ? setting->mulf(drive) : 1;
  319. div_factor = setting->divf ? setting->divf(drive) : 1;
  320. ide_write_setting(drive, setting, val * div_factor / mul_factor);
  321. }
  322. mutex_unlock(&ide_setting_mtx);
  323. }
  324. } while (!for_real++);
  325. free_page((unsigned long)buf);
  326. return count;
  327. parse_error:
  328. free_page((unsigned long)buf);
  329. printk("%s(): parse error\n", __func__);
  330. return -EINVAL;
  331. }
  332. static const struct file_operations ide_settings_proc_fops = {
  333. .owner = THIS_MODULE,
  334. .open = ide_settings_proc_open,
  335. .read = seq_read,
  336. .llseek = seq_lseek,
  337. .release = single_release,
  338. .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_fops,
  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. static int ide_drivers_open(struct inode *inode, struct file *file)
  531. {
  532. return single_open(file, &ide_drivers_show, NULL);
  533. }
  534. static const struct file_operations ide_drivers_operations = {
  535. .owner = THIS_MODULE,
  536. .open = ide_drivers_open,
  537. .read = seq_read,
  538. .llseek = seq_lseek,
  539. .release = single_release,
  540. };
  541. void proc_ide_create(void)
  542. {
  543. proc_ide_root = proc_mkdir("ide", NULL);
  544. if (!proc_ide_root)
  545. return;
  546. proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
  547. }
  548. void proc_ide_destroy(void)
  549. {
  550. remove_proc_entry("drivers", proc_ide_root);
  551. remove_proc_entry("ide", NULL);
  552. }