ide-proc.c 14 KB

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