ide-proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/module.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/errno.h>
  26. #include <linux/sched.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/stat.h>
  29. #include <linux/mm.h>
  30. #include <linux/pci.h>
  31. #include <linux/ctype.h>
  32. #include <linux/hdreg.h>
  33. #include <linux/ide.h>
  34. #include <linux/seq_file.h>
  35. #include <asm/io.h>
  36. static int proc_ide_read_imodel
  37. (char *page, char **start, off_t off, int count, int *eof, void *data)
  38. {
  39. ide_hwif_t *hwif = (ide_hwif_t *) data;
  40. int len;
  41. const char *name;
  42. /*
  43. * Neither ide_unknown nor ide_forced should be set at this point.
  44. */
  45. switch (hwif->chipset) {
  46. case ide_generic: name = "generic"; break;
  47. case ide_pci: name = "pci"; break;
  48. case ide_cmd640: name = "cmd640"; break;
  49. case ide_dtc2278: name = "dtc2278"; break;
  50. case ide_ali14xx: name = "ali14xx"; break;
  51. case ide_qd65xx: name = "qd65xx"; break;
  52. case ide_umc8672: name = "umc8672"; break;
  53. case ide_ht6560b: name = "ht6560b"; break;
  54. case ide_rz1000: name = "rz1000"; break;
  55. case ide_trm290: name = "trm290"; break;
  56. case ide_cmd646: name = "cmd646"; break;
  57. case ide_cy82c693: name = "cy82c693"; break;
  58. case ide_4drives: name = "4drives"; break;
  59. case ide_pmac: name = "mac-io"; break;
  60. case ide_au1xxx: name = "au1xxx"; 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. int err;
  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. err = device_attach(dev);
  291. if (err < 0)
  292. printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
  293. __FUNCTION__, err);
  294. drive->driver_req[0] = 0;
  295. if (dev->driver == NULL) {
  296. err = device_attach(dev);
  297. if (err < 0)
  298. printk(KERN_WARNING
  299. "IDE: %s: device_attach(2) error: %d\n",
  300. __FUNCTION__, err);
  301. }
  302. if (dev->driver && !strcmp(dev->driver->name, driver))
  303. ret = 0;
  304. up_write(&dev->bus->subsys.rwsem);
  305. return ret;
  306. }
  307. static int proc_ide_write_driver
  308. (struct file *file, const char __user *buffer, unsigned long count, void *data)
  309. {
  310. ide_drive_t *drive = (ide_drive_t *) data;
  311. char name[32];
  312. if (!capable(CAP_SYS_ADMIN))
  313. return -EACCES;
  314. if (count > 31)
  315. count = 31;
  316. if (copy_from_user(name, buffer, count))
  317. return -EFAULT;
  318. name[count] = '\0';
  319. if (ide_replace_subdriver(drive, name))
  320. return -EINVAL;
  321. return count;
  322. }
  323. static int proc_ide_read_media
  324. (char *page, char **start, off_t off, int count, int *eof, void *data)
  325. {
  326. ide_drive_t *drive = (ide_drive_t *) data;
  327. const char *media;
  328. int len;
  329. switch (drive->media) {
  330. case ide_disk: media = "disk\n";
  331. break;
  332. case ide_cdrom: media = "cdrom\n";
  333. break;
  334. case ide_tape: media = "tape\n";
  335. break;
  336. case ide_floppy:media = "floppy\n";
  337. break;
  338. case ide_optical:media = "optical\n";
  339. break;
  340. default: media = "UNKNOWN\n";
  341. break;
  342. }
  343. strcpy(page,media);
  344. len = strlen(media);
  345. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  346. }
  347. static ide_proc_entry_t generic_drive_entries[] = {
  348. { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
  349. { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
  350. { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
  351. { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
  352. { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
  353. { NULL, 0, NULL, NULL }
  354. };
  355. void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
  356. {
  357. struct proc_dir_entry *ent;
  358. if (!dir || !p)
  359. return;
  360. while (p->name != NULL) {
  361. ent = create_proc_entry(p->name, p->mode, dir);
  362. if (!ent) return;
  363. ent->nlink = 1;
  364. ent->data = data;
  365. ent->read_proc = p->read_proc;
  366. ent->write_proc = p->write_proc;
  367. p++;
  368. }
  369. }
  370. void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
  371. {
  372. if (!dir || !p)
  373. return;
  374. while (p->name != NULL) {
  375. remove_proc_entry(p->name, dir);
  376. p++;
  377. }
  378. }
  379. static void create_proc_ide_drives(ide_hwif_t *hwif)
  380. {
  381. int d;
  382. struct proc_dir_entry *ent;
  383. struct proc_dir_entry *parent = hwif->proc;
  384. char name[64];
  385. for (d = 0; d < MAX_DRIVES; d++) {
  386. ide_drive_t *drive = &hwif->drives[d];
  387. if (!drive->present)
  388. continue;
  389. if (drive->proc)
  390. continue;
  391. drive->proc = proc_mkdir(drive->name, parent);
  392. if (drive->proc)
  393. ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
  394. sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
  395. ent = proc_symlink(drive->name, proc_ide_root, name);
  396. if (!ent) return;
  397. }
  398. }
  399. static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
  400. {
  401. if (drive->proc) {
  402. ide_remove_proc_entries(drive->proc, generic_drive_entries);
  403. remove_proc_entry(drive->name, proc_ide_root);
  404. remove_proc_entry(drive->name, hwif->proc);
  405. drive->proc = NULL;
  406. }
  407. }
  408. static void destroy_proc_ide_drives(ide_hwif_t *hwif)
  409. {
  410. int d;
  411. for (d = 0; d < MAX_DRIVES; d++) {
  412. ide_drive_t *drive = &hwif->drives[d];
  413. if (drive->proc)
  414. destroy_proc_ide_device(hwif, drive);
  415. }
  416. }
  417. static ide_proc_entry_t hwif_entries[] = {
  418. { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
  419. { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
  420. { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
  421. { NULL, 0, NULL, NULL }
  422. };
  423. void create_proc_ide_interfaces(void)
  424. {
  425. int h;
  426. for (h = 0; h < MAX_HWIFS; h++) {
  427. ide_hwif_t *hwif = &ide_hwifs[h];
  428. if (!hwif->present)
  429. continue;
  430. if (!hwif->proc) {
  431. hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
  432. if (!hwif->proc)
  433. return;
  434. ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
  435. }
  436. create_proc_ide_drives(hwif);
  437. }
  438. }
  439. EXPORT_SYMBOL(create_proc_ide_interfaces);
  440. #ifdef CONFIG_BLK_DEV_IDEPCI
  441. void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
  442. {
  443. create_proc_info_entry(name, 0, proc_ide_root, get_info);
  444. }
  445. EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
  446. #endif
  447. void destroy_proc_ide_interface(ide_hwif_t *hwif)
  448. {
  449. if (hwif->proc) {
  450. destroy_proc_ide_drives(hwif);
  451. ide_remove_proc_entries(hwif->proc, hwif_entries);
  452. remove_proc_entry(hwif->name, proc_ide_root);
  453. hwif->proc = NULL;
  454. }
  455. }
  456. static int proc_print_driver(struct device_driver *drv, void *data)
  457. {
  458. ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
  459. struct seq_file *s = data;
  460. seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
  461. return 0;
  462. }
  463. static int ide_drivers_show(struct seq_file *s, void *p)
  464. {
  465. int err;
  466. err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
  467. if (err < 0)
  468. printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
  469. __FUNCTION__, err);
  470. return 0;
  471. }
  472. static int ide_drivers_open(struct inode *inode, struct file *file)
  473. {
  474. return single_open(file, &ide_drivers_show, NULL);
  475. }
  476. static struct file_operations ide_drivers_operations = {
  477. .open = ide_drivers_open,
  478. .read = seq_read,
  479. .llseek = seq_lseek,
  480. .release = single_release,
  481. };
  482. void proc_ide_create(void)
  483. {
  484. struct proc_dir_entry *entry;
  485. if (!proc_ide_root)
  486. return;
  487. create_proc_ide_interfaces();
  488. entry = create_proc_entry("drivers", 0, proc_ide_root);
  489. if (entry)
  490. entry->proc_fops = &ide_drivers_operations;
  491. }
  492. void proc_ide_destroy(void)
  493. {
  494. remove_proc_entry("drivers", proc_ide_root);
  495. remove_proc_entry("ide", NULL);
  496. }