ide-proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * linux/drivers/ide/ide-proc.c Version 1.05 Mar 05, 2003
  3. *
  4. * Copyright (C) 1997-1998 Mark Lord
  5. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  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. * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
  16. * smart_thresholds, capabilities]" will issue an IDENTIFY /
  17. * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
  18. * SENSE CAPABILITIES command to /dev/hda, and then dump out the
  19. * returned data as 256 16-bit words. The "hdparm" utility will
  20. * be updated someday soon to use this mechanism.
  21. *
  22. */
  23. #include <linux/config.h>
  24. #include <linux/module.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/errno.h>
  27. #include <linux/sched.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/stat.h>
  30. #include <linux/mm.h>
  31. #include <linux/pci.h>
  32. #include <linux/ctype.h>
  33. #include <linux/hdreg.h>
  34. #include <linux/ide.h>
  35. #include <linux/seq_file.h>
  36. #include <asm/io.h>
  37. static int proc_ide_read_imodel
  38. (char *page, char **start, off_t off, int count, int *eof, void *data)
  39. {
  40. ide_hwif_t *hwif = (ide_hwif_t *) data;
  41. int len;
  42. const char *name;
  43. /*
  44. * Neither ide_unknown nor ide_forced should be set at this point.
  45. */
  46. switch (hwif->chipset) {
  47. case ide_generic: name = "generic"; break;
  48. case ide_pci: name = "pci"; break;
  49. case ide_cmd640: name = "cmd640"; break;
  50. case ide_dtc2278: name = "dtc2278"; break;
  51. case ide_ali14xx: name = "ali14xx"; break;
  52. case ide_qd65xx: name = "qd65xx"; break;
  53. case ide_umc8672: name = "umc8672"; break;
  54. case ide_ht6560b: name = "ht6560b"; break;
  55. case ide_rz1000: name = "rz1000"; break;
  56. case ide_trm290: name = "trm290"; break;
  57. case ide_cmd646: name = "cmd646"; break;
  58. case ide_cy82c693: name = "cy82c693"; break;
  59. case ide_4drives: name = "4drives"; break;
  60. case ide_pmac: name = "mac-io"; break;
  61. default: name = "(unknown)"; break;
  62. }
  63. len = sprintf(page, "%s\n", name);
  64. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  65. }
  66. static int proc_ide_read_mate
  67. (char *page, char **start, off_t off, int count, int *eof, void *data)
  68. {
  69. ide_hwif_t *hwif = (ide_hwif_t *) data;
  70. int len;
  71. if (hwif && hwif->mate && hwif->mate->present)
  72. len = sprintf(page, "%s\n", hwif->mate->name);
  73. else
  74. len = sprintf(page, "(none)\n");
  75. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  76. }
  77. static int proc_ide_read_channel
  78. (char *page, char **start, off_t off, int count, int *eof, void *data)
  79. {
  80. ide_hwif_t *hwif = (ide_hwif_t *) data;
  81. int len;
  82. page[0] = hwif->channel ? '1' : '0';
  83. page[1] = '\n';
  84. len = 2;
  85. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  86. }
  87. static int proc_ide_read_identify
  88. (char *page, char **start, off_t off, int count, int *eof, void *data)
  89. {
  90. ide_drive_t *drive = (ide_drive_t *)data;
  91. int len = 0, i = 0;
  92. int err = 0;
  93. len = sprintf(page, "\n");
  94. if (drive) {
  95. unsigned short *val = (unsigned short *) page;
  96. err = taskfile_lib_get_identify(drive, page);
  97. if (!err) {
  98. char *out = ((char *)page) + (SECTOR_WORDS * 4);
  99. page = out;
  100. do {
  101. out += sprintf(out, "%04x%c",
  102. le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
  103. val += 1;
  104. } while (i < (SECTOR_WORDS * 2));
  105. len = out - page;
  106. }
  107. }
  108. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  109. }
  110. static void proc_ide_settings_warn(void)
  111. {
  112. static int warned = 0;
  113. if (warned)
  114. return;
  115. printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
  116. "obsolete, and will be removed soon!\n");
  117. warned = 1;
  118. }
  119. static int proc_ide_read_settings
  120. (char *page, char **start, off_t off, int count, int *eof, void *data)
  121. {
  122. ide_drive_t *drive = (ide_drive_t *) data;
  123. ide_settings_t *setting = (ide_settings_t *) drive->settings;
  124. char *out = page;
  125. int len, rc, mul_factor, div_factor;
  126. proc_ide_settings_warn();
  127. down(&ide_setting_sem);
  128. out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
  129. out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
  130. while(setting) {
  131. mul_factor = setting->mul_factor;
  132. div_factor = setting->div_factor;
  133. out += sprintf(out, "%-24s", setting->name);
  134. if ((rc = ide_read_setting(drive, setting)) >= 0)
  135. out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
  136. else
  137. out += sprintf(out, "%-16s", "write-only");
  138. out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
  139. if (setting->rw & SETTING_READ)
  140. out += sprintf(out, "r");
  141. if (setting->rw & SETTING_WRITE)
  142. out += sprintf(out, "w");
  143. out += sprintf(out, "\n");
  144. setting = setting->next;
  145. }
  146. len = out - page;
  147. up(&ide_setting_sem);
  148. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  149. }
  150. #define MAX_LEN 30
  151. static int proc_ide_write_settings(struct file *file, const char __user *buffer,
  152. unsigned long count, void *data)
  153. {
  154. ide_drive_t *drive = (ide_drive_t *) data;
  155. char name[MAX_LEN + 1];
  156. int for_real = 0;
  157. unsigned long n;
  158. ide_settings_t *setting;
  159. char *buf, *s;
  160. if (!capable(CAP_SYS_ADMIN))
  161. return -EACCES;
  162. proc_ide_settings_warn();
  163. if (count >= PAGE_SIZE)
  164. return -EINVAL;
  165. s = buf = (char *)__get_free_page(GFP_USER);
  166. if (!buf)
  167. return -ENOMEM;
  168. if (copy_from_user(buf, buffer, count)) {
  169. free_page((unsigned long)buf);
  170. return -EFAULT;
  171. }
  172. buf[count] = '\0';
  173. /*
  174. * Skip over leading whitespace
  175. */
  176. while (count && isspace(*s)) {
  177. --count;
  178. ++s;
  179. }
  180. /*
  181. * Do one full pass to verify all parameters,
  182. * then do another to actually write the new settings.
  183. */
  184. do {
  185. char *p = s;
  186. n = count;
  187. while (n > 0) {
  188. unsigned val;
  189. char *q = p;
  190. while (n > 0 && *p != ':') {
  191. --n;
  192. p++;
  193. }
  194. if (*p != ':')
  195. goto parse_error;
  196. if (p - q > MAX_LEN)
  197. goto parse_error;
  198. memcpy(name, q, p - q);
  199. name[p - q] = 0;
  200. if (n > 0) {
  201. --n;
  202. p++;
  203. } else
  204. goto parse_error;
  205. val = simple_strtoul(p, &q, 10);
  206. n -= q - p;
  207. p = q;
  208. if (n > 0 && !isspace(*p))
  209. goto parse_error;
  210. while (n > 0 && isspace(*p)) {
  211. --n;
  212. ++p;
  213. }
  214. down(&ide_setting_sem);
  215. setting = ide_find_setting_by_name(drive, name);
  216. if (!setting)
  217. {
  218. up(&ide_setting_sem);
  219. goto parse_error;
  220. }
  221. if (for_real)
  222. ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
  223. up(&ide_setting_sem);
  224. }
  225. } while (!for_real++);
  226. free_page((unsigned long)buf);
  227. return count;
  228. parse_error:
  229. free_page((unsigned long)buf);
  230. printk("proc_ide_write_settings(): parse error\n");
  231. return -EINVAL;
  232. }
  233. int proc_ide_read_capacity
  234. (char *page, char **start, off_t off, int count, int *eof, void *data)
  235. {
  236. int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
  237. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  238. }
  239. EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
  240. int proc_ide_read_geometry
  241. (char *page, char **start, off_t off, int count, int *eof, void *data)
  242. {
  243. ide_drive_t *drive = (ide_drive_t *) data;
  244. char *out = page;
  245. int len;
  246. out += sprintf(out,"physical %d/%d/%d\n",
  247. drive->cyl, drive->head, drive->sect);
  248. out += sprintf(out,"logical %d/%d/%d\n",
  249. drive->bios_cyl, drive->bios_head, drive->bios_sect);
  250. len = out - page;
  251. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  252. }
  253. EXPORT_SYMBOL(proc_ide_read_geometry);
  254. static int proc_ide_read_dmodel
  255. (char *page, char **start, off_t off, int count, int *eof, void *data)
  256. {
  257. ide_drive_t *drive = (ide_drive_t *) data;
  258. struct hd_driveid *id = drive->id;
  259. int len;
  260. len = sprintf(page, "%.40s\n",
  261. (id && id->model[0]) ? (char *)id->model : "(none)");
  262. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  263. }
  264. static int proc_ide_read_driver
  265. (char *page, char **start, off_t off, int count, int *eof, void *data)
  266. {
  267. ide_drive_t *drive = (ide_drive_t *) data;
  268. struct device *dev = &drive->gendev;
  269. ide_driver_t *ide_drv;
  270. int len;
  271. down_read(&dev->bus->subsys.rwsem);
  272. if (dev->driver) {
  273. ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
  274. len = sprintf(page, "%s version %s\n",
  275. dev->driver->name, ide_drv->version);
  276. } else
  277. len = sprintf(page, "ide-default version 0.9.newide\n");
  278. up_read(&dev->bus->subsys.rwsem);
  279. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  280. }
  281. static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
  282. {
  283. struct device *dev = &drive->gendev;
  284. int ret = 1;
  285. down_write(&dev->bus->subsys.rwsem);
  286. device_release_driver(dev);
  287. /* FIXME: device can still be in use by previous driver */
  288. strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
  289. device_attach(dev);
  290. drive->driver_req[0] = 0;
  291. if (dev->driver == NULL)
  292. device_attach(dev);
  293. if (dev->driver && !strcmp(dev->driver->name, driver))
  294. ret = 0;
  295. up_write(&dev->bus->subsys.rwsem);
  296. return ret;
  297. }
  298. static int proc_ide_write_driver
  299. (struct file *file, const char __user *buffer, unsigned long count, void *data)
  300. {
  301. ide_drive_t *drive = (ide_drive_t *) data;
  302. char name[32];
  303. if (!capable(CAP_SYS_ADMIN))
  304. return -EACCES;
  305. if (count > 31)
  306. count = 31;
  307. if (copy_from_user(name, buffer, count))
  308. return -EFAULT;
  309. name[count] = '\0';
  310. if (ide_replace_subdriver(drive, name))
  311. return -EINVAL;
  312. return count;
  313. }
  314. static int proc_ide_read_media
  315. (char *page, char **start, off_t off, int count, int *eof, void *data)
  316. {
  317. ide_drive_t *drive = (ide_drive_t *) data;
  318. const char *media;
  319. int len;
  320. switch (drive->media) {
  321. case ide_disk: media = "disk\n";
  322. break;
  323. case ide_cdrom: media = "cdrom\n";
  324. break;
  325. case ide_tape: media = "tape\n";
  326. break;
  327. case ide_floppy:media = "floppy\n";
  328. break;
  329. default: media = "UNKNOWN\n";
  330. break;
  331. }
  332. strcpy(page,media);
  333. len = strlen(media);
  334. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  335. }
  336. static ide_proc_entry_t generic_drive_entries[] = {
  337. { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
  338. { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
  339. { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
  340. { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
  341. { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
  342. { NULL, 0, NULL, NULL }
  343. };
  344. void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
  345. {
  346. struct proc_dir_entry *ent;
  347. if (!dir || !p)
  348. return;
  349. while (p->name != NULL) {
  350. ent = create_proc_entry(p->name, p->mode, dir);
  351. if (!ent) return;
  352. ent->nlink = 1;
  353. ent->data = data;
  354. ent->read_proc = p->read_proc;
  355. ent->write_proc = p->write_proc;
  356. p++;
  357. }
  358. }
  359. void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
  360. {
  361. if (!dir || !p)
  362. return;
  363. while (p->name != NULL) {
  364. remove_proc_entry(p->name, dir);
  365. p++;
  366. }
  367. }
  368. static void create_proc_ide_drives(ide_hwif_t *hwif)
  369. {
  370. int d;
  371. struct proc_dir_entry *ent;
  372. struct proc_dir_entry *parent = hwif->proc;
  373. char name[64];
  374. for (d = 0; d < MAX_DRIVES; d++) {
  375. ide_drive_t *drive = &hwif->drives[d];
  376. if (!drive->present)
  377. continue;
  378. if (drive->proc)
  379. continue;
  380. drive->proc = proc_mkdir(drive->name, parent);
  381. if (drive->proc)
  382. ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
  383. sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
  384. ent = proc_symlink(drive->name, proc_ide_root, name);
  385. if (!ent) return;
  386. }
  387. }
  388. static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
  389. {
  390. if (drive->proc) {
  391. ide_remove_proc_entries(drive->proc, generic_drive_entries);
  392. remove_proc_entry(drive->name, proc_ide_root);
  393. remove_proc_entry(drive->name, hwif->proc);
  394. drive->proc = NULL;
  395. }
  396. }
  397. static void destroy_proc_ide_drives(ide_hwif_t *hwif)
  398. {
  399. int d;
  400. for (d = 0; d < MAX_DRIVES; d++) {
  401. ide_drive_t *drive = &hwif->drives[d];
  402. if (drive->proc)
  403. destroy_proc_ide_device(hwif, drive);
  404. }
  405. }
  406. static ide_proc_entry_t hwif_entries[] = {
  407. { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
  408. { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
  409. { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
  410. { NULL, 0, NULL, NULL }
  411. };
  412. void create_proc_ide_interfaces(void)
  413. {
  414. int h;
  415. for (h = 0; h < MAX_HWIFS; h++) {
  416. ide_hwif_t *hwif = &ide_hwifs[h];
  417. if (!hwif->present)
  418. continue;
  419. if (!hwif->proc) {
  420. hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
  421. if (!hwif->proc)
  422. return;
  423. ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
  424. }
  425. create_proc_ide_drives(hwif);
  426. }
  427. }
  428. EXPORT_SYMBOL(create_proc_ide_interfaces);
  429. #ifdef CONFIG_BLK_DEV_IDEPCI
  430. void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
  431. {
  432. create_proc_info_entry(name, 0, proc_ide_root, get_info);
  433. }
  434. EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
  435. #endif
  436. void destroy_proc_ide_interface(ide_hwif_t *hwif)
  437. {
  438. if (hwif->proc) {
  439. destroy_proc_ide_drives(hwif);
  440. ide_remove_proc_entries(hwif->proc, hwif_entries);
  441. remove_proc_entry(hwif->name, proc_ide_root);
  442. hwif->proc = NULL;
  443. }
  444. }
  445. static int proc_print_driver(struct device_driver *drv, void *data)
  446. {
  447. ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
  448. struct seq_file *s = data;
  449. seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
  450. return 0;
  451. }
  452. static int ide_drivers_show(struct seq_file *s, void *p)
  453. {
  454. bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
  455. return 0;
  456. }
  457. static int ide_drivers_open(struct inode *inode, struct file *file)
  458. {
  459. return single_open(file, &ide_drivers_show, NULL);
  460. }
  461. static struct file_operations ide_drivers_operations = {
  462. .open = ide_drivers_open,
  463. .read = seq_read,
  464. .llseek = seq_lseek,
  465. .release = single_release,
  466. };
  467. void proc_ide_create(void)
  468. {
  469. struct proc_dir_entry *entry;
  470. if (!proc_ide_root)
  471. return;
  472. create_proc_ide_interfaces();
  473. entry = create_proc_entry("drivers", 0, proc_ide_root);
  474. if (entry)
  475. entry->proc_fops = &ide_drivers_operations;
  476. }
  477. void proc_ide_destroy(void)
  478. {
  479. remove_proc_entry("drivers", proc_ide_root);
  480. remove_proc_entry("ide", NULL);
  481. }