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