ide-proc.c 22 KB

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