ide-proc.c 14 KB

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