ide-proc.c 22 KB

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