blob: 84665e2ba3c85844436c1a1970d3ce6e7686987e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
8/*
9 * This is the /proc/ide/ filesystem implementation.
10 *
11 * Drive/Driver settings can be retrieved by reading the drive's
12 * "settings" files. e.g. "cat /proc/ide0/hda/settings"
13 * To write a new value "val" into a specific setting "name", use:
14 * echo "name:val" >/proc/ide/ide0/hda/settings
15 *
16 * Also useful, "cat /proc/ide0/hda/[identify, smart_values,
17 * smart_thresholds, capabilities]" will issue an IDENTIFY /
18 * PACKET_IDENTIFY / SMART_READ_VALUES / SMART_READ_THRESHOLDS /
19 * SENSE CAPABILITIES command to /dev/hda, and then dump out the
20 * returned data as 256 16-bit words. The "hdparm" utility will
21 * be updated someday soon to use this mechanism.
22 *
23 */
24
25#include <linux/config.h>
26#include <linux/module.h>
27
28#include <asm/uaccess.h>
29#include <linux/errno.h>
30#include <linux/sched.h>
31#include <linux/proc_fs.h>
32#include <linux/stat.h>
33#include <linux/mm.h>
34#include <linux/pci.h>
35#include <linux/ctype.h>
36#include <linux/hdreg.h>
37#include <linux/ide.h>
38#include <linux/seq_file.h>
39
40#include <asm/io.h>
41
42static int proc_ide_read_imodel
43 (char *page, char **start, off_t off, int count, int *eof, void *data)
44{
45 ide_hwif_t *hwif = (ide_hwif_t *) data;
46 int len;
47 const char *name;
48
49 /*
50 * Neither ide_unknown nor ide_forced should be set at this point.
51 */
52 switch (hwif->chipset) {
53 case ide_generic: name = "generic"; break;
54 case ide_pci: name = "pci"; break;
55 case ide_cmd640: name = "cmd640"; break;
56 case ide_dtc2278: name = "dtc2278"; break;
57 case ide_ali14xx: name = "ali14xx"; break;
58 case ide_qd65xx: name = "qd65xx"; break;
59 case ide_umc8672: name = "umc8672"; break;
60 case ide_ht6560b: name = "ht6560b"; break;
61 case ide_rz1000: name = "rz1000"; break;
62 case ide_trm290: name = "trm290"; break;
63 case ide_cmd646: name = "cmd646"; break;
64 case ide_cy82c693: name = "cy82c693"; break;
65 case ide_4drives: name = "4drives"; break;
66 case ide_pmac: name = "mac-io"; break;
Pete Popov26a940e2005-09-15 08:03:12 +000067 case ide_au1xxx: name = "au1xxx"; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 default: name = "(unknown)"; break;
69 }
70 len = sprintf(page, "%s\n", name);
71 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
72}
73
74static int proc_ide_read_mate
75 (char *page, char **start, off_t off, int count, int *eof, void *data)
76{
77 ide_hwif_t *hwif = (ide_hwif_t *) data;
78 int len;
79
80 if (hwif && hwif->mate && hwif->mate->present)
81 len = sprintf(page, "%s\n", hwif->mate->name);
82 else
83 len = sprintf(page, "(none)\n");
84 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
85}
86
87static int proc_ide_read_channel
88 (char *page, char **start, off_t off, int count, int *eof, void *data)
89{
90 ide_hwif_t *hwif = (ide_hwif_t *) data;
91 int len;
92
93 page[0] = hwif->channel ? '1' : '0';
94 page[1] = '\n';
95 len = 2;
96 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
97}
98
99static int proc_ide_read_identify
100 (char *page, char **start, off_t off, int count, int *eof, void *data)
101{
102 ide_drive_t *drive = (ide_drive_t *)data;
103 int len = 0, i = 0;
104 int err = 0;
105
106 len = sprintf(page, "\n");
107
108 if (drive) {
109 unsigned short *val = (unsigned short *) page;
110
111 err = taskfile_lib_get_identify(drive, page);
112 if (!err) {
113 char *out = ((char *)page) + (SECTOR_WORDS * 4);
114 page = out;
115 do {
116 out += sprintf(out, "%04x%c",
117 le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
118 val += 1;
119 } while (i < (SECTOR_WORDS * 2));
120 len = out - page;
121 }
122 }
123 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
124}
125
126static void proc_ide_settings_warn(void)
127{
128 static int warned = 0;
129
130 if (warned)
131 return;
132
133 printk(KERN_WARNING "Warning: /proc/ide/hd?/settings interface is "
134 "obsolete, and will be removed soon!\n");
135 warned = 1;
136}
137
138static int proc_ide_read_settings
139 (char *page, char **start, off_t off, int count, int *eof, void *data)
140{
141 ide_drive_t *drive = (ide_drive_t *) data;
142 ide_settings_t *setting = (ide_settings_t *) drive->settings;
143 char *out = page;
144 int len, rc, mul_factor, div_factor;
145
146 proc_ide_settings_warn();
147
148 down(&ide_setting_sem);
149 out += sprintf(out, "name\t\t\tvalue\t\tmin\t\tmax\t\tmode\n");
150 out += sprintf(out, "----\t\t\t-----\t\t---\t\t---\t\t----\n");
151 while(setting) {
152 mul_factor = setting->mul_factor;
153 div_factor = setting->div_factor;
154 out += sprintf(out, "%-24s", setting->name);
155 if ((rc = ide_read_setting(drive, setting)) >= 0)
156 out += sprintf(out, "%-16d", rc * mul_factor / div_factor);
157 else
158 out += sprintf(out, "%-16s", "write-only");
159 out += sprintf(out, "%-16d%-16d", (setting->min * mul_factor + div_factor - 1) / div_factor, setting->max * mul_factor / div_factor);
160 if (setting->rw & SETTING_READ)
161 out += sprintf(out, "r");
162 if (setting->rw & SETTING_WRITE)
163 out += sprintf(out, "w");
164 out += sprintf(out, "\n");
165 setting = setting->next;
166 }
167 len = out - page;
168 up(&ide_setting_sem);
169 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
170}
171
172#define MAX_LEN 30
173
174static int proc_ide_write_settings(struct file *file, const char __user *buffer,
175 unsigned long count, void *data)
176{
177 ide_drive_t *drive = (ide_drive_t *) data;
178 char name[MAX_LEN + 1];
179 int for_real = 0;
180 unsigned long n;
181 ide_settings_t *setting;
182 char *buf, *s;
183
184 if (!capable(CAP_SYS_ADMIN))
185 return -EACCES;
186
187 proc_ide_settings_warn();
188
189 if (count >= PAGE_SIZE)
190 return -EINVAL;
191
192 s = buf = (char *)__get_free_page(GFP_USER);
193 if (!buf)
194 return -ENOMEM;
195
196 if (copy_from_user(buf, buffer, count)) {
197 free_page((unsigned long)buf);
198 return -EFAULT;
199 }
200
201 buf[count] = '\0';
202
203 /*
204 * Skip over leading whitespace
205 */
206 while (count && isspace(*s)) {
207 --count;
208 ++s;
209 }
210 /*
211 * Do one full pass to verify all parameters,
212 * then do another to actually write the new settings.
213 */
214 do {
215 char *p = s;
216 n = count;
217 while (n > 0) {
218 unsigned val;
219 char *q = p;
220
221 while (n > 0 && *p != ':') {
222 --n;
223 p++;
224 }
225 if (*p != ':')
226 goto parse_error;
227 if (p - q > MAX_LEN)
228 goto parse_error;
229 memcpy(name, q, p - q);
230 name[p - q] = 0;
231
232 if (n > 0) {
233 --n;
234 p++;
235 } else
236 goto parse_error;
237
238 val = simple_strtoul(p, &q, 10);
239 n -= q - p;
240 p = q;
241 if (n > 0 && !isspace(*p))
242 goto parse_error;
243 while (n > 0 && isspace(*p)) {
244 --n;
245 ++p;
246 }
247
248 down(&ide_setting_sem);
249 setting = ide_find_setting_by_name(drive, name);
250 if (!setting)
251 {
252 up(&ide_setting_sem);
253 goto parse_error;
254 }
255 if (for_real)
256 ide_write_setting(drive, setting, val * setting->div_factor / setting->mul_factor);
257 up(&ide_setting_sem);
258 }
259 } while (!for_real++);
260 free_page((unsigned long)buf);
261 return count;
262parse_error:
263 free_page((unsigned long)buf);
264 printk("proc_ide_write_settings(): parse error\n");
265 return -EINVAL;
266}
267
268int proc_ide_read_capacity
269 (char *page, char **start, off_t off, int count, int *eof, void *data)
270{
271 int len = sprintf(page,"%llu\n", (long long)0x7fffffff);
272 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
273}
274
275EXPORT_SYMBOL_GPL(proc_ide_read_capacity);
276
277int proc_ide_read_geometry
278 (char *page, char **start, off_t off, int count, int *eof, void *data)
279{
280 ide_drive_t *drive = (ide_drive_t *) data;
281 char *out = page;
282 int len;
283
284 out += sprintf(out,"physical %d/%d/%d\n",
285 drive->cyl, drive->head, drive->sect);
286 out += sprintf(out,"logical %d/%d/%d\n",
287 drive->bios_cyl, drive->bios_head, drive->bios_sect);
288
289 len = out - page;
290 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
291}
292
293EXPORT_SYMBOL(proc_ide_read_geometry);
294
295static int proc_ide_read_dmodel
296 (char *page, char **start, off_t off, int count, int *eof, void *data)
297{
298 ide_drive_t *drive = (ide_drive_t *) data;
299 struct hd_driveid *id = drive->id;
300 int len;
301
302 len = sprintf(page, "%.40s\n",
303 (id && id->model[0]) ? (char *)id->model : "(none)");
304 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
305}
306
307static int proc_ide_read_driver
308 (char *page, char **start, off_t off, int count, int *eof, void *data)
309{
310 ide_drive_t *drive = (ide_drive_t *) data;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200311 struct device *dev = &drive->gendev;
312 ide_driver_t *ide_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int len;
314
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200315 down_read(&dev->bus->subsys.rwsem);
316 if (dev->driver) {
317 ide_drv = container_of(dev->driver, ide_driver_t, gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 len = sprintf(page, "%s version %s\n",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200319 dev->driver->name, ide_drv->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else
321 len = sprintf(page, "ide-default version 0.9.newide\n");
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200322 up_read(&dev->bus->subsys.rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
324}
325
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200326static int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
327{
328 struct device *dev = &drive->gendev;
329 int ret = 1;
330
331 down_write(&dev->bus->subsys.rwsem);
332 device_release_driver(dev);
333 /* FIXME: device can still be in use by previous driver */
334 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
335 device_attach(dev);
336 drive->driver_req[0] = 0;
337 if (dev->driver == NULL)
338 device_attach(dev);
339 if (dev->driver && !strcmp(dev->driver->name, driver))
340 ret = 0;
341 up_write(&dev->bus->subsys.rwsem);
342
343 return ret;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static int proc_ide_write_driver
347 (struct file *file, const char __user *buffer, unsigned long count, void *data)
348{
349 ide_drive_t *drive = (ide_drive_t *) data;
350 char name[32];
351
352 if (!capable(CAP_SYS_ADMIN))
353 return -EACCES;
354 if (count > 31)
355 count = 31;
356 if (copy_from_user(name, buffer, count))
357 return -EFAULT;
358 name[count] = '\0';
359 if (ide_replace_subdriver(drive, name))
360 return -EINVAL;
361 return count;
362}
363
364static int proc_ide_read_media
365 (char *page, char **start, off_t off, int count, int *eof, void *data)
366{
367 ide_drive_t *drive = (ide_drive_t *) data;
368 const char *media;
369 int len;
370
371 switch (drive->media) {
372 case ide_disk: media = "disk\n";
373 break;
374 case ide_cdrom: media = "cdrom\n";
375 break;
376 case ide_tape: media = "tape\n";
377 break;
378 case ide_floppy:media = "floppy\n";
379 break;
380 default: media = "UNKNOWN\n";
381 break;
382 }
383 strcpy(page,media);
384 len = strlen(media);
385 PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
386}
387
388static ide_proc_entry_t generic_drive_entries[] = {
389 { "driver", S_IFREG|S_IRUGO, proc_ide_read_driver, proc_ide_write_driver },
390 { "identify", S_IFREG|S_IRUSR, proc_ide_read_identify, NULL },
391 { "media", S_IFREG|S_IRUGO, proc_ide_read_media, NULL },
392 { "model", S_IFREG|S_IRUGO, proc_ide_read_dmodel, NULL },
393 { "settings", S_IFREG|S_IRUSR|S_IWUSR,proc_ide_read_settings, proc_ide_write_settings },
394 { NULL, 0, NULL, NULL }
395};
396
397void ide_add_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p, void *data)
398{
399 struct proc_dir_entry *ent;
400
401 if (!dir || !p)
402 return;
403 while (p->name != NULL) {
404 ent = create_proc_entry(p->name, p->mode, dir);
405 if (!ent) return;
406 ent->nlink = 1;
407 ent->data = data;
408 ent->read_proc = p->read_proc;
409 ent->write_proc = p->write_proc;
410 p++;
411 }
412}
413
414void ide_remove_proc_entries(struct proc_dir_entry *dir, ide_proc_entry_t *p)
415{
416 if (!dir || !p)
417 return;
418 while (p->name != NULL) {
419 remove_proc_entry(p->name, dir);
420 p++;
421 }
422}
423
424static void create_proc_ide_drives(ide_hwif_t *hwif)
425{
426 int d;
427 struct proc_dir_entry *ent;
428 struct proc_dir_entry *parent = hwif->proc;
429 char name[64];
430
431 for (d = 0; d < MAX_DRIVES; d++) {
432 ide_drive_t *drive = &hwif->drives[d];
433
434 if (!drive->present)
435 continue;
436 if (drive->proc)
437 continue;
438
439 drive->proc = proc_mkdir(drive->name, parent);
440 if (drive->proc)
441 ide_add_proc_entries(drive->proc, generic_drive_entries, drive);
442 sprintf(name,"ide%d/%s", (drive->name[2]-'a')/2, drive->name);
443 ent = proc_symlink(drive->name, proc_ide_root, name);
444 if (!ent) return;
445 }
446}
447
448static void destroy_proc_ide_device(ide_hwif_t *hwif, ide_drive_t *drive)
449{
450 if (drive->proc) {
451 ide_remove_proc_entries(drive->proc, generic_drive_entries);
452 remove_proc_entry(drive->name, proc_ide_root);
453 remove_proc_entry(drive->name, hwif->proc);
454 drive->proc = NULL;
455 }
456}
457
458static void destroy_proc_ide_drives(ide_hwif_t *hwif)
459{
460 int d;
461
462 for (d = 0; d < MAX_DRIVES; d++) {
463 ide_drive_t *drive = &hwif->drives[d];
464 if (drive->proc)
465 destroy_proc_ide_device(hwif, drive);
466 }
467}
468
469static ide_proc_entry_t hwif_entries[] = {
470 { "channel", S_IFREG|S_IRUGO, proc_ide_read_channel, NULL },
471 { "mate", S_IFREG|S_IRUGO, proc_ide_read_mate, NULL },
472 { "model", S_IFREG|S_IRUGO, proc_ide_read_imodel, NULL },
473 { NULL, 0, NULL, NULL }
474};
475
476void create_proc_ide_interfaces(void)
477{
478 int h;
479
480 for (h = 0; h < MAX_HWIFS; h++) {
481 ide_hwif_t *hwif = &ide_hwifs[h];
482
483 if (!hwif->present)
484 continue;
485 if (!hwif->proc) {
486 hwif->proc = proc_mkdir(hwif->name, proc_ide_root);
487 if (!hwif->proc)
488 return;
489 ide_add_proc_entries(hwif->proc, hwif_entries, hwif);
490 }
491 create_proc_ide_drives(hwif);
492 }
493}
494
495EXPORT_SYMBOL(create_proc_ide_interfaces);
496
497#ifdef CONFIG_BLK_DEV_IDEPCI
498void ide_pci_create_host_proc(const char *name, get_info_t *get_info)
499{
500 create_proc_info_entry(name, 0, proc_ide_root, get_info);
501}
502
503EXPORT_SYMBOL_GPL(ide_pci_create_host_proc);
504#endif
505
506void destroy_proc_ide_interface(ide_hwif_t *hwif)
507{
508 if (hwif->proc) {
509 destroy_proc_ide_drives(hwif);
510 ide_remove_proc_entries(hwif->proc, hwif_entries);
511 remove_proc_entry(hwif->name, proc_ide_root);
512 hwif->proc = NULL;
513 }
514}
515
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200516static int proc_print_driver(struct device_driver *drv, void *data)
517{
518 ide_driver_t *ide_drv = container_of(drv, ide_driver_t, gen_driver);
519 struct seq_file *s = data;
520
521 seq_printf(s, "%s version %s\n", drv->name, ide_drv->version);
522
523 return 0;
524}
525
526static int ide_drivers_show(struct seq_file *s, void *p)
527{
528 bus_for_each_drv(&ide_bus_type, NULL, s, proc_print_driver);
529 return 0;
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532static int ide_drivers_open(struct inode *inode, struct file *file)
533{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200534 return single_open(file, &ide_drivers_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static struct file_operations ide_drivers_operations = {
538 .open = ide_drivers_open,
539 .read = seq_read,
540 .llseek = seq_lseek,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200541 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
544void proc_ide_create(void)
545{
546 struct proc_dir_entry *entry;
547
548 if (!proc_ide_root)
549 return;
550
551 create_proc_ide_interfaces();
552
553 entry = create_proc_entry("drivers", 0, proc_ide_root);
554 if (entry)
555 entry->proc_fops = &ide_drivers_operations;
556}
557
558void proc_ide_destroy(void)
559{
Zhang, Yanmin643bdc62005-05-16 21:53:11 -0700560 remove_proc_entry("drivers", proc_ide_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 remove_proc_entry("ide", NULL);
562}