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