ide-proc.c 23 KB

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