ide-proc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright (C) 1997-1998 Mark Lord
  3. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  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 <asm/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 <asm/io.h>
  26. static struct proc_dir_entry *proc_ide_root;
  27. static int proc_ide_read_imodel
  28. (char *page, char **start, off_t off, int count, int *eof, void *data)
  29. {
  30. ide_hwif_t *hwif = (ide_hwif_t *) data;
  31. int len;
  32. const char *name;
  33. switch (hwif->chipset) {
  34. case ide_generic: name = "generic"; break;
  35. case ide_pci: name = "pci"; break;
  36. case ide_cmd640: name = "cmd640"; break;
  37. case ide_dtc2278: name = "dtc2278"; break;
  38. case ide_ali14xx: name = "ali14xx"; break;
  39. case ide_qd65xx: name = "qd65xx"; break;
  40. case ide_umc8672: name = "umc8672"; break;
  41. case ide_ht6560b: name = "ht6560b"; break;
  42. case ide_rz1000: name = "rz1000"; break;
  43. case ide_trm290: name = "trm290"; break;
  44. case ide_cmd646: name = "cmd646"; break;
  45. case ide_cy82c693: name = "cy82c693"; break;
  46. case ide_4drives: name = "4drives"; break;
  47. case ide_pmac: name = "mac-io"; break;
  48. case ide_au1xxx: name = "au1xxx"; break;
  49. case ide_palm3710: name = "palm3710"; break;
  50. case ide_acorn: name = "acorn"; break;
  51. default: name = "(unknown)"; break;
  52. }
  53. len = sprintf(page, "%s\n", name);
  54. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  55. }
  56. static int proc_ide_read_mate
  57. (char *page, char **start, off_t off, int count, int *eof, void *data)
  58. {
  59. ide_hwif_t *hwif = (ide_hwif_t *) data;
  60. int len;
  61. if (hwif && hwif->mate)
  62. len = sprintf(page, "%s\n", hwif->mate->name);
  63. else
  64. len = sprintf(page, "(none)\n");
  65. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  66. }
  67. static int proc_ide_read_channel
  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. page[0] = hwif->channel ? '1' : '0';
  73. page[1] = '\n';
  74. len = 2;
  75. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  76. }
  77. static int proc_ide_read_identify
  78. (char *page, char **start, off_t off, int count, int *eof, void *data)
  79. {
  80. ide_drive_t *drive = (ide_drive_t *)data;
  81. int len = 0, i = 0;
  82. int err = 0;
  83. len = sprintf(page, "\n");
  84. if (drive) {
  85. __le16 *val = (__le16 *)page;
  86. err = taskfile_lib_get_identify(drive, page);
  87. if (!err) {
  88. char *out = ((char *)page) + (SECTOR_WORDS * 4);
  89. page = out;
  90. do {
  91. out += sprintf(out, "%04x%c",
  92. le16_to_cpup(val), (++i & 7) ? ' ' : '\n');
  93. val += 1;
  94. } while (i < (SECTOR_WORDS * 2));
  95. len = out - page;
  96. }
  97. }
  98. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  99. }
  100. /**
  101. * __ide_add_setting - add an ide setting option
  102. * @drive: drive to use
  103. * @name: setting name
  104. * @rw: true if the function is read write
  105. * @data_type: type of data
  106. * @min: range minimum
  107. * @max: range maximum
  108. * @mul_factor: multiplication scale
  109. * @div_factor: divison scale
  110. * @data: private data field
  111. * @set: setting
  112. * @auto_remove: setting auto removal flag
  113. *
  114. * Removes the setting named from the device if it is present.
  115. * The function takes the settings_lock to protect against
  116. * parallel changes. This function must not be called from IRQ
  117. * context. Returns 0 on success or -1 on failure.
  118. *
  119. * BUGS: This code is seriously over-engineered. There is also
  120. * magic about how the driver specific features are setup. If
  121. * a driver is attached we assume the driver settings are auto
  122. * remove.
  123. */
  124. static int __ide_add_setting(ide_drive_t *drive, const char *name, int rw, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set, int auto_remove)
  125. {
  126. ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
  127. mutex_lock(&ide_setting_mtx);
  128. while ((*p) && strcmp((*p)->name, name) < 0)
  129. p = &((*p)->next);
  130. if ((setting = kzalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
  131. goto abort;
  132. if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
  133. goto abort;
  134. strcpy(setting->name, name);
  135. setting->rw = rw;
  136. setting->data_type = data_type;
  137. setting->min = min;
  138. setting->max = max;
  139. setting->mul_factor = mul_factor;
  140. setting->div_factor = div_factor;
  141. setting->data = data;
  142. setting->set = set;
  143. setting->next = *p;
  144. if (auto_remove)
  145. setting->auto_remove = 1;
  146. *p = setting;
  147. mutex_unlock(&ide_setting_mtx);
  148. return 0;
  149. abort:
  150. mutex_unlock(&ide_setting_mtx);
  151. kfree(setting);
  152. return -1;
  153. }
  154. int ide_add_setting(ide_drive_t *drive, const char *name, int rw, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
  155. {
  156. return __ide_add_setting(drive, name, rw, data_type, min, max, mul_factor, div_factor, data, set, 1);
  157. }
  158. EXPORT_SYMBOL(ide_add_setting);
  159. /**
  160. * __ide_remove_setting - remove an ide setting option
  161. * @drive: drive to use
  162. * @name: setting name
  163. *
  164. * Removes the setting named from the device if it is present.
  165. * The caller must hold the setting semaphore.
  166. */
  167. static void __ide_remove_setting(ide_drive_t *drive, char *name)
  168. {
  169. ide_settings_t **p, *setting;
  170. p = (ide_settings_t **) &drive->settings;
  171. while ((*p) && strcmp((*p)->name, name))
  172. p = &((*p)->next);
  173. setting = (*p);
  174. if (setting == NULL)
  175. return;
  176. (*p) = setting->next;
  177. kfree(setting->name);
  178. kfree(setting);
  179. }
  180. /**
  181. * auto_remove_settings - remove driver specific settings
  182. * @drive: drive
  183. *
  184. * Automatically remove all the driver specific settings for this
  185. * drive. This function may not be called from IRQ context. The
  186. * caller must hold ide_setting_mtx.
  187. */
  188. static void auto_remove_settings(ide_drive_t *drive)
  189. {
  190. ide_settings_t *setting;
  191. repeat:
  192. setting = drive->settings;
  193. while (setting) {
  194. if (setting->auto_remove) {
  195. __ide_remove_setting(drive, setting->name);
  196. goto repeat;
  197. }
  198. setting = setting->next;
  199. }
  200. }
  201. /**
  202. * ide_find_setting_by_name - find a drive specific setting
  203. * @drive: drive to scan
  204. * @name: setting name
  205. *
  206. * Scan's the device setting table for a matching entry and returns
  207. * this or NULL if no entry is found. The caller must hold the
  208. * setting semaphore
  209. */
  210. static ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
  211. {
  212. ide_settings_t *setting = drive->settings;
  213. while (setting) {
  214. if (strcmp(setting->name, name) == 0)
  215. break;
  216. setting = setting->next;
  217. }
  218. return setting;
  219. }
  220. /**
  221. * ide_read_setting - read an IDE setting
  222. * @drive: drive to read from
  223. * @setting: drive setting
  224. *
  225. * Read a drive setting and return the value. The caller
  226. * must hold the ide_setting_mtx when making this call.
  227. *
  228. * BUGS: the data return and error are the same return value
  229. * so an error -EINVAL and true return of the same value cannot
  230. * be told apart
  231. */
  232. static int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
  233. {
  234. int val = -EINVAL;
  235. unsigned long flags;
  236. if ((setting->rw & SETTING_READ)) {
  237. spin_lock_irqsave(&ide_lock, flags);
  238. switch (setting->data_type) {
  239. case TYPE_BYTE:
  240. val = *((u8 *) setting->data);
  241. break;
  242. case TYPE_SHORT:
  243. val = *((u16 *) setting->data);
  244. break;
  245. case TYPE_INT:
  246. val = *((u32 *) setting->data);
  247. break;
  248. }
  249. spin_unlock_irqrestore(&ide_lock, flags);
  250. }
  251. return val;
  252. }
  253. /**
  254. * ide_write_setting - read an IDE setting
  255. * @drive: drive to read from
  256. * @setting: drive setting
  257. * @val: value
  258. *
  259. * Write a drive setting if it is possible. The caller
  260. * must hold the ide_setting_mtx when making this call.
  261. *
  262. * BUGS: the data return and error are the same return value
  263. * so an error -EINVAL and true return of the same value cannot
  264. * be told apart
  265. *
  266. * FIXME: This should be changed to enqueue a special request
  267. * to the driver to change settings, and then wait on a sema for completion.
  268. * The current scheme of polling is kludgy, though safe enough.
  269. */
  270. static int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
  271. {
  272. if (!capable(CAP_SYS_ADMIN))
  273. return -EACCES;
  274. if (setting->set)
  275. return setting->set(drive, val);
  276. if (!(setting->rw & SETTING_WRITE))
  277. return -EPERM;
  278. if (val < setting->min || val > setting->max)
  279. return -EINVAL;
  280. if (ide_spin_wait_hwgroup(drive))
  281. return -EBUSY;
  282. switch (setting->data_type) {
  283. case TYPE_BYTE:
  284. *((u8 *) setting->data) = val;
  285. break;
  286. case TYPE_SHORT:
  287. *((u16 *) setting->data) = val;
  288. break;
  289. case TYPE_INT:
  290. *((u32 *) setting->data) = val;
  291. break;
  292. }
  293. spin_unlock_irq(&ide_lock);
  294. return 0;
  295. }
  296. static int set_xfer_rate (ide_drive_t *drive, int arg)
  297. {
  298. ide_task_t task;
  299. int err;
  300. if (arg < XFER_PIO_0 || arg > XFER_UDMA_6)
  301. return -EINVAL;
  302. memset(&task, 0, sizeof(task));
  303. task.tf.command = ATA_CMD_SET_FEATURES;
  304. task.tf.feature = SETFEATURES_XFER;
  305. task.tf.nsect = (u8)arg;
  306. task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT |
  307. IDE_TFLAG_IN_NSECT;
  308. err = ide_no_data_taskfile(drive, &task);
  309. if (!err) {
  310. ide_set_xfer_rate(drive, (u8) arg);
  311. ide_driveid_update(drive);
  312. }
  313. return err;
  314. }
  315. /**
  316. * ide_add_generic_settings - generic ide settings
  317. * @drive: drive being configured
  318. *
  319. * Add the generic parts of the system settings to the /proc files.
  320. * The caller must not be holding the ide_setting_mtx.
  321. */
  322. void ide_add_generic_settings (ide_drive_t *drive)
  323. {
  324. /*
  325. * drive setting name read/write access data type min max mul_factor div_factor data pointer set function
  326. */
  327. __ide_add_setting(drive, "io_32bit", SETTING_RW, TYPE_BYTE, 0, 1 + (SUPPORT_VLB_SYNC << 1), 1, 1, &drive->io_32bit, set_io_32bit, 0);
  328. __ide_add_setting(drive, "keepsettings", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, set_ksettings, 0);
  329. __ide_add_setting(drive, "nice1", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL, 0);
  330. __ide_add_setting(drive, "pio_mode", SETTING_WRITE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode, 0);
  331. __ide_add_setting(drive, "unmaskirq", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->unmask, set_unmaskirq, 0);
  332. __ide_add_setting(drive, "using_dma", SETTING_RW, TYPE_BYTE, 0, 1, 1, 1, &drive->using_dma, set_using_dma, 0);
  333. __ide_add_setting(drive, "init_speed", SETTING_RW, TYPE_BYTE, 0, 70, 1, 1, &drive->init_speed, NULL, 0);
  334. __ide_add_setting(drive, "current_speed", SETTING_RW, TYPE_BYTE, 0, 70, 1, 1, &drive->current_speed, set_xfer_rate, 0);
  335. __ide_add_setting(drive, "number", SETTING_RW, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL, 0);
  336. }
  337. static void proc_ide_settings_warn(void)
  338. {
  339. static int warned;
  340. if (warned)
  341. return;
  342. printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
  343. "obsolete, and will be removed soon!\n");
  344. warned = 1;
  345. }
  346. static int proc_ide_read_settings
  347. (char *page, char **start, off_t off, int count, int *eof, void *data)
  348. {
  349. ide_drive_t *drive = (ide_drive_t *) data;
  350. ide_settings_t *setting = (ide_settings_t *) drive->settings;
  351. char *out = page;
  352. int len, rc, mul_factor, div_factor;
  353. proc_ide_settings_warn();
  354. mutex_lock(&ide_setting_mtx);
  355. out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
  356. out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
  357. while (setting) {
  358. mul_factor = setting->mul_factor;
  359. div_factor = setting->div_factor;
  360. out += sprintf(out, "%-24s", setting->name);
  361. rc = ide_read_setting(drive, setting);
  362. if (rc >= 0)
  363. out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
  364. else
  365. out += sprintf(out, "%-16s", "write-only");
  366. out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
  367. if (setting->rw & SETTING_READ)
  368. out += sprintf(out, "r");
  369. if (setting->rw & SETTING_WRITE)
  370. out += sprintf(out, "w");
  371. out += sprintf(out, "\n");
  372. setting = setting->next;
  373. }
  374. len = out - page;
  375. mutex_unlock(&ide_setting_mtx);
  376. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  377. }
  378. #define MAX_LEN 30
  379. static int proc_ide_write_settings(struct file *file, const char __user *buffer,
  380. unsigned long count, void *data)
  381. {
  382. ide_drive_t *drive = (ide_drive_t *) data;
  383. char name[MAX_LEN + 1];
  384. int for_real = 0;
  385. unsigned long n;
  386. ide_settings_t *setting;
  387. char *buf, *s;
  388. if (!capable(CAP_SYS_ADMIN))
  389. return -EACCES;
  390. proc_ide_settings_warn();
  391. if (count >= PAGE_SIZE)
  392. return -EINVAL;
  393. s = buf = (char *)__get_free_page(GFP_USER);
  394. if (!buf)
  395. return -ENOMEM;
  396. if (copy_from_user(buf, buffer, count)) {
  397. free_page((unsigned long)buf);
  398. return -EFAULT;
  399. }
  400. buf[count] = '\0';
  401. /*
  402. * Skip over leading whitespace
  403. */
  404. while (count && isspace(*s)) {
  405. --count;
  406. ++s;
  407. }
  408. /*
  409. * Do one full pass to verify all parameters,
  410. * then do another to actually write the new settings.
  411. */
  412. do {
  413. char *p = s;
  414. n = count;
  415. while (n > 0) {
  416. unsigned val;
  417. char *q = p;
  418. while (n > 0 && *p != ':') {
  419. --n;
  420. p++;
  421. }
  422. if (*p != ':')
  423. goto parse_error;
  424. if (p - q > MAX_LEN)
  425. goto parse_error;
  426. memcpy(name, q, p - q);
  427. name[p - q] = 0;
  428. if (n > 0) {
  429. --n;
  430. p++;
  431. } else
  432. goto parse_error;
  433. val = simple_strtoul(p, &q, 10);
  434. n -= q - p;
  435. p = q;
  436. if (n > 0 && !isspace(*p))
  437. goto parse_error;
  438. while (n > 0 && isspace(*p)) {
  439. --n;
  440. ++p;
  441. }
  442. mutex_lock(&ide_setting_mtx);
  443. setting = ide_find_setting_by_name(drive, name);
  444. if (!setting) {
  445. mutex_unlock(&ide_setting_mtx);
  446. goto parse_error;
  447. }
  448. if (for_real)
  449. ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
  450. mutex_unlock(&ide_setting_mtx);
  451. }
  452. } while (!for_real++);
  453. free_page((unsigned long)buf);
  454. return count;
  455. parse_error:
  456. free_page((unsigned long)buf);
  457. printk("proc_ide_write_settings(): parse error\n");
  458. return -EINVAL;
  459. }
  460. int proc_ide_read_capacity
  461. (char *page, char **start, off_t off, int count, int *eof, void *data)
  462. {
  463. int len = sprintf(page, "%llu\n", (long long)0x7fffffff);
  464. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  465. }
  466. EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
  467. int proc_ide_read_geometry
  468. (char *page, char **start, off_t off, int count, int *eof, void *data)
  469. {
  470. ide_drive_t *drive = (ide_drive_t *) data;
  471. char *out = page;
  472. int len;
  473. out += sprintf(out, "physical %d/%d/%d\n",
  474. drive->cyl, drive->head, drive->sect);
  475. out += sprintf(out, "logical %d/%d/%d\n",
  476. drive->bios_cyl, drive->bios_head, drive->bios_sect);
  477. len = out - page;
  478. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  479. }
  480. EXPORT_SYMBOL(proc_ide_read_geometry);
  481. static int proc_ide_read_dmodel
  482. (char *page, char **start, off_t off, int count, int *eof, void *data)
  483. {
  484. ide_drive_t *drive = (ide_drive_t *) data;
  485. char *m = (char *)&drive->id[ATA_ID_PROD];
  486. int len;
  487. len = sprintf(page, "%.40s\n", m[0] ? m : "(none)");
  488. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  489. }
  490. static int proc_ide_read_driver
  491. (char *page, char **start, off_t off, int count, int *eof, void *data)
  492. {
  493. ide_drive_t *drive = (ide_drive_t *) data;
  494. struct device *dev = &drive->gendev;
  495. ide_driver_t *ide_drv;
  496. int len;
  497. if (dev->driver) {
  498. ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
  499. len = sprintf(page, "%s version %s\n",
  500. dev->driver->name, ide_drv->version);
  501. } else
  502. len = sprintf(page, "ide-default version 0.9.newide\n");
  503. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  504. }
  505. static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
  506. {
  507. struct device *dev = &drive->gendev;
  508. int ret = 1;
  509. int err;
  510. device_release_driver(dev);
  511. /* FIXME: device can still be in use by previous driver */
  512. strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
  513. err = device_attach(dev);
  514. if (err < 0)
  515. printk(KERN_WARNING "IDE: %s: device_attach error: %d\n",
  516. __func__, err);
  517. drive->driver_req[0] = 0;
  518. if (dev->driver == NULL) {
  519. err = device_attach(dev);
  520. if (err < 0)
  521. printk(KERN_WARNING
  522. "IDE: %s: device_attach(2) error: %d\n",
  523. __func__, err);
  524. }
  525. if (dev->driver && !strcmp(dev->driver->name, driver))
  526. ret = 0;
  527. return ret;
  528. }
  529. static int proc_ide_write_driver
  530. (struct file *file, const char __user *buffer, unsigned long count, void *data)
  531. {
  532. ide_drive_t *drive = (ide_drive_t *) data;
  533. char name[32];
  534. if (!capable(CAP_SYS_ADMIN))
  535. return -EACCES;
  536. if (count > 31)
  537. count = 31;
  538. if (copy_from_user(name, buffer, count))
  539. return -EFAULT;
  540. name[count] = '\0';
  541. if (ide_replace_subdriver(drive, name))
  542. return -EINVAL;
  543. return count;
  544. }
  545. static int proc_ide_read_media
  546. (char *page, char **start, off_t off, int count, int *eof, void *data)
  547. {
  548. ide_drive_t *drive = (ide_drive_t *) data;
  549. const char *media;
  550. int len;
  551. switch (drive->media) {
  552. case ide_disk: media = "disk\n"; break;
  553. case ide_cdrom: media = "cdrom\n"; break;
  554. case ide_tape: media = "tape\n"; break;
  555. case ide_floppy: media = "floppy\n"; break;
  556. case ide_optical: media = "optical\n"; break;
  557. default: media = "UNKNOWN\n"; break;
  558. }
  559. strcpy(page, media);
  560. len = strlen(media);
  561. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  562. }
  563. static ide_proc_entry_t generic_drive_entries[] = {
  564. { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver,
  565. proc_ide_write_driver },
  566. { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
  567. { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
  568. { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
  569. { "settings", S_IFREG|S_IRUSR|S_IWUSR, proc_ide_read_settings,
  570. proc_ide_write_settings },
  571. { NULL, 0, NULL, NULL }
  572. };
  573. static void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
  574. {
  575. struct proc_dir_entry *ent;
  576. if (!dir || !p)
  577. return;
  578. while (p->name != NULL) {
  579. ent = create_proc_entry(p->name, p->mode, dir);
  580. if (!ent) return;
  581. ent->data = data;
  582. ent->read_proc = p->read_proc;
  583. ent->write_proc = p->write_proc;
  584. p++;
  585. }
  586. }
  587. static void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
  588. {
  589. if (!dir || !p)
  590. return;
  591. while (p->name != NULL) {
  592. remove_proc_entry(p->name, dir);
  593. p++;
  594. }
  595. }
  596. void ide_proc_register_driver(ide_drive_t *drive, ide_driver_t *driver)
  597. {
  598. ide_add_proc_entries(drive->proc, driver->proc, drive);
  599. }
  600. EXPORT_SYMBOL(ide_proc_register_driver);
  601. /**
  602. * ide_proc_unregister_driver - remove driver specific data
  603. * @drive: drive
  604. * @driver: driver
  605. *
  606. * Clean up the driver specific /proc files and IDE settings
  607. * for a given drive.
  608. *
  609. * Takes ide_setting_mtx and ide_lock.
  610. * Caller must hold none of the locks.
  611. */
  612. void ide_proc_unregister_driver(ide_drive_t *drive, ide_driver_t *driver)
  613. {
  614. unsigned long flags;
  615. ide_remove_proc_entries(drive->proc, driver->proc);
  616. mutex_lock(&ide_setting_mtx);
  617. spin_lock_irqsave(&ide_lock, flags);
  618. /*
  619. * ide_setting_mtx protects the settings list
  620. * ide_lock protects the use of settings
  621. *
  622. * so we need to hold both, ide_settings_sem because we want to
  623. * modify the settings list, and ide_lock because we cannot take
  624. * a setting out that is being used.
  625. *
  626. * OTOH both ide_{read,write}_setting are only ever used under
  627. * ide_setting_mtx.
  628. */
  629. auto_remove_settings(drive);
  630. spin_unlock_irqrestore(&ide_lock, flags);
  631. mutex_unlock(&ide_setting_mtx);
  632. }
  633. EXPORT_SYMBOL(ide_proc_unregister_driver);
  634. void ide_proc_port_register_devices(ide_hwif_t *hwif)
  635. {
  636. int d;
  637. struct proc_dir_entry *ent;
  638. struct proc_dir_entry *parent = hwif->proc;
  639. char name[64];
  640. for (d = 0; d < MAX_DRIVES; d++) {
  641. ide_drive_t *drive = &hwif->drives[d];
  642. if (!drive->present)
  643. continue;
  644. if (drive->proc)
  645. continue;
  646. drive->proc = proc_mkdir(drive->name, parent);
  647. if (drive->proc)
  648. ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
  649. sprintf(name, "ide%d/%s", (drive->name[2]-'a')/2, drive->name);
  650. ent = proc_symlink(drive->name, proc_ide_root, name);
  651. if (!ent) return;
  652. }
  653. }
  654. void ide_proc_unregister_device(ide_drive_t *drive)
  655. {
  656. if (drive->proc) {
  657. ide_remove_proc_entries(drive->proc, generic_drive_entries);
  658. remove_proc_entry(drive->name, proc_ide_root);
  659. remove_proc_entry(drive->name, drive->hwif->proc);
  660. drive->proc = NULL;
  661. }
  662. }
  663. static ide_proc_entry_t hwif_entries[] = {
  664. { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
  665. { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
  666. { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
  667. { NULL, 0, NULL, NULL }
  668. };
  669. void ide_proc_register_port(ide_hwif_t *hwif)
  670. {
  671. if (!hwif->proc) {
  672. hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
  673. if (!hwif->proc)
  674. return;
  675. ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
  676. }
  677. }
  678. void ide_proc_unregister_port(ide_hwif_t *hwif)
  679. {
  680. if (hwif->proc) {
  681. ide_remove_proc_entries(hwif->proc, hwif_entries);
  682. remove_proc_entry(hwif->name, proc_ide_root);
  683. hwif->proc = NULL;
  684. }
  685. }
  686. static int proc_print_driver(struct device_driver *drv, void *data)
  687. {
  688. ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
  689. struct seq_file *s = data;
  690. seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
  691. return 0;
  692. }
  693. static int ide_drivers_show(struct seq_file *s, void *p)
  694. {
  695. int err;
  696. err = bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
  697. if (err < 0)
  698. printk(KERN_WARNING "IDE: %s: bus_for_each_drv error: %d\n",
  699. __func__, err);
  700. return 0;
  701. }
  702. static int ide_drivers_open(struct inode *inode, struct file *file)
  703. {
  704. return single_open(file, &ide_drivers_show, NULL);
  705. }
  706. static const struct file_operations ide_drivers_operations = {
  707. .owner = THIS_MODULE,
  708. .open = ide_drivers_open,
  709. .read = seq_read,
  710. .llseek = seq_lseek,
  711. .release = single_release,
  712. };
  713. void proc_ide_create(void)
  714. {
  715. proc_ide_root = proc_mkdir("ide", NULL);
  716. if (!proc_ide_root)
  717. return;
  718. proc_create("drivers", 0, proc_ide_root, &ide_drivers_operations);
  719. }
  720. void proc_ide_destroy(void)
  721. {
  722. remove_proc_entry("drivers", proc_ide_root);
  723. remove_proc_entry("ide", NULL);
  724. }