interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * interface.c - contains everything related to the user interface
  3. *
  4. * Some code, especially possible resource dumping is based on isapnp_proc.c (c) Jaroslav Kysela <perex@perex.cz>
  5. * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
  6. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  7. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  8. */
  9. #include <linux/pnp.h>
  10. #include <linux/string.h>
  11. #include <linux/errno.h>
  12. #include <linux/list.h>
  13. #include <linux/types.h>
  14. #include <linux/stat.h>
  15. #include <linux/ctype.h>
  16. #include <linux/slab.h>
  17. #include <linux/mutex.h>
  18. #include <asm/uaccess.h>
  19. #include "base.h"
  20. struct pnp_info_buffer {
  21. char *buffer; /* pointer to begin of buffer */
  22. char *curr; /* current position in buffer */
  23. unsigned long size; /* current size */
  24. unsigned long len; /* total length of buffer */
  25. int stop; /* stop flag */
  26. int error; /* error code */
  27. };
  28. typedef struct pnp_info_buffer pnp_info_buffer_t;
  29. static int pnp_printf(pnp_info_buffer_t * buffer, char *fmt, ...)
  30. {
  31. va_list args;
  32. int res;
  33. if (buffer->stop || buffer->error)
  34. return 0;
  35. va_start(args, fmt);
  36. res = vsnprintf(buffer->curr, buffer->len - buffer->size, fmt, args);
  37. va_end(args);
  38. if (buffer->size + res >= buffer->len) {
  39. buffer->stop = 1;
  40. return 0;
  41. }
  42. buffer->curr += res;
  43. buffer->size += res;
  44. return res;
  45. }
  46. static void pnp_print_port(pnp_info_buffer_t * buffer, char *space,
  47. struct pnp_port *port)
  48. {
  49. pnp_printf(buffer, "%sport %#llx-%#llx, align %#llx, size %#llx, "
  50. "%i-bit address decoding\n", space,
  51. (unsigned long long) port->min,
  52. (unsigned long long) port->max,
  53. port->align ? ((unsigned long long) port->align - 1) : 0,
  54. (unsigned long long) port->size,
  55. port->flags & IORESOURCE_IO_16BIT_ADDR ? 16 : 10);
  56. }
  57. static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
  58. struct pnp_irq *irq)
  59. {
  60. int first = 1, i;
  61. pnp_printf(buffer, "%sirq ", space);
  62. for (i = 0; i < PNP_IRQ_NR; i++)
  63. if (test_bit(i, irq->map.bits)) {
  64. if (!first) {
  65. pnp_printf(buffer, ",");
  66. } else {
  67. first = 0;
  68. }
  69. if (i == 2 || i == 9)
  70. pnp_printf(buffer, "2/9");
  71. else
  72. pnp_printf(buffer, "%i", i);
  73. }
  74. if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
  75. pnp_printf(buffer, "<none>");
  76. if (irq->flags & IORESOURCE_IRQ_HIGHEDGE)
  77. pnp_printf(buffer, " High-Edge");
  78. if (irq->flags & IORESOURCE_IRQ_LOWEDGE)
  79. pnp_printf(buffer, " Low-Edge");
  80. if (irq->flags & IORESOURCE_IRQ_HIGHLEVEL)
  81. pnp_printf(buffer, " High-Level");
  82. if (irq->flags & IORESOURCE_IRQ_LOWLEVEL)
  83. pnp_printf(buffer, " Low-Level");
  84. if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
  85. pnp_printf(buffer, " (optional)");
  86. pnp_printf(buffer, "\n");
  87. }
  88. static void pnp_print_dma(pnp_info_buffer_t * buffer, char *space,
  89. struct pnp_dma *dma)
  90. {
  91. int first = 1, i;
  92. char *s;
  93. pnp_printf(buffer, "%sdma ", space);
  94. for (i = 0; i < 8; i++)
  95. if (dma->map & (1 << i)) {
  96. if (!first) {
  97. pnp_printf(buffer, ",");
  98. } else {
  99. first = 0;
  100. }
  101. pnp_printf(buffer, "%i", i);
  102. }
  103. if (!dma->map)
  104. pnp_printf(buffer, "<none>");
  105. switch (dma->flags & IORESOURCE_DMA_TYPE_MASK) {
  106. case IORESOURCE_DMA_8BIT:
  107. s = "8-bit";
  108. break;
  109. case IORESOURCE_DMA_8AND16BIT:
  110. s = "8-bit&16-bit";
  111. break;
  112. default:
  113. s = "16-bit";
  114. }
  115. pnp_printf(buffer, " %s", s);
  116. if (dma->flags & IORESOURCE_DMA_MASTER)
  117. pnp_printf(buffer, " master");
  118. if (dma->flags & IORESOURCE_DMA_BYTE)
  119. pnp_printf(buffer, " byte-count");
  120. if (dma->flags & IORESOURCE_DMA_WORD)
  121. pnp_printf(buffer, " word-count");
  122. switch (dma->flags & IORESOURCE_DMA_SPEED_MASK) {
  123. case IORESOURCE_DMA_TYPEA:
  124. s = "type-A";
  125. break;
  126. case IORESOURCE_DMA_TYPEB:
  127. s = "type-B";
  128. break;
  129. case IORESOURCE_DMA_TYPEF:
  130. s = "type-F";
  131. break;
  132. default:
  133. s = "compatible";
  134. break;
  135. }
  136. pnp_printf(buffer, " %s\n", s);
  137. }
  138. static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
  139. struct pnp_mem *mem)
  140. {
  141. char *s;
  142. pnp_printf(buffer, "%sMemory %#llx-%#llx, align %#llx, size %#llx",
  143. space, (unsigned long long) mem->min,
  144. (unsigned long long) mem->max,
  145. (unsigned long long) mem->align,
  146. (unsigned long long) mem->size);
  147. if (mem->flags & IORESOURCE_MEM_WRITEABLE)
  148. pnp_printf(buffer, ", writeable");
  149. if (mem->flags & IORESOURCE_MEM_CACHEABLE)
  150. pnp_printf(buffer, ", cacheable");
  151. if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
  152. pnp_printf(buffer, ", range-length");
  153. if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
  154. pnp_printf(buffer, ", shadowable");
  155. if (mem->flags & IORESOURCE_MEM_EXPANSIONROM)
  156. pnp_printf(buffer, ", expansion ROM");
  157. switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
  158. case IORESOURCE_MEM_8BIT:
  159. s = "8-bit";
  160. break;
  161. case IORESOURCE_MEM_8AND16BIT:
  162. s = "8-bit&16-bit";
  163. break;
  164. case IORESOURCE_MEM_32BIT:
  165. s = "32-bit";
  166. break;
  167. default:
  168. s = "16-bit";
  169. }
  170. pnp_printf(buffer, ", %s\n", s);
  171. }
  172. static void pnp_print_option(pnp_info_buffer_t * buffer, char *space,
  173. struct pnp_option *option)
  174. {
  175. switch (option->type) {
  176. case IORESOURCE_IO:
  177. pnp_print_port(buffer, space, &option->u.port);
  178. break;
  179. case IORESOURCE_MEM:
  180. pnp_print_mem(buffer, space, &option->u.mem);
  181. break;
  182. case IORESOURCE_IRQ:
  183. pnp_print_irq(buffer, space, &option->u.irq);
  184. break;
  185. case IORESOURCE_DMA:
  186. pnp_print_dma(buffer, space, &option->u.dma);
  187. break;
  188. }
  189. }
  190. static ssize_t options_show(struct device *dmdev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct pnp_dev *dev = to_pnp_dev(dmdev);
  194. pnp_info_buffer_t *buffer;
  195. struct pnp_option *option;
  196. int ret, dep = 0, set = 0;
  197. char *indent;
  198. buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
  199. if (!buffer)
  200. return -ENOMEM;
  201. buffer->len = PAGE_SIZE;
  202. buffer->buffer = buf;
  203. buffer->curr = buffer->buffer;
  204. list_for_each_entry(option, &dev->options, list) {
  205. if (pnp_option_is_dependent(option)) {
  206. indent = " ";
  207. if (!dep || pnp_option_set(option) != set) {
  208. set = pnp_option_set(option);
  209. dep = 1;
  210. pnp_printf(buffer, "Dependent: %02i - "
  211. "Priority %s\n", set,
  212. pnp_option_priority_name(option));
  213. }
  214. } else {
  215. dep = 0;
  216. indent = "";
  217. }
  218. pnp_print_option(buffer, indent, option);
  219. }
  220. ret = (buffer->curr - buf);
  221. kfree(buffer);
  222. return ret;
  223. }
  224. static DEVICE_ATTR_RO(options);
  225. static ssize_t resources_show(struct device *dmdev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct pnp_dev *dev = to_pnp_dev(dmdev);
  229. pnp_info_buffer_t *buffer;
  230. struct pnp_resource *pnp_res;
  231. struct resource *res;
  232. int ret;
  233. if (!dev)
  234. return -EINVAL;
  235. buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
  236. if (!buffer)
  237. return -ENOMEM;
  238. buffer->len = PAGE_SIZE;
  239. buffer->buffer = buf;
  240. buffer->curr = buffer->buffer;
  241. pnp_printf(buffer, "state = %s\n", dev->active ? "active" : "disabled");
  242. list_for_each_entry(pnp_res, &dev->resources, list) {
  243. res = &pnp_res->res;
  244. pnp_printf(buffer, pnp_resource_type_name(res));
  245. if (res->flags & IORESOURCE_DISABLED) {
  246. pnp_printf(buffer, " disabled\n");
  247. continue;
  248. }
  249. switch (pnp_resource_type(res)) {
  250. case IORESOURCE_IO:
  251. case IORESOURCE_MEM:
  252. case IORESOURCE_BUS:
  253. pnp_printf(buffer, " %#llx-%#llx%s\n",
  254. (unsigned long long) res->start,
  255. (unsigned long long) res->end,
  256. res->flags & IORESOURCE_WINDOW ?
  257. " window" : "");
  258. break;
  259. case IORESOURCE_IRQ:
  260. case IORESOURCE_DMA:
  261. pnp_printf(buffer, " %lld\n",
  262. (unsigned long long) res->start);
  263. break;
  264. }
  265. }
  266. ret = (buffer->curr - buf);
  267. kfree(buffer);
  268. return ret;
  269. }
  270. static char *pnp_get_resource_value(char *buf,
  271. unsigned long type,
  272. resource_size_t *start,
  273. resource_size_t *end,
  274. unsigned long *flags)
  275. {
  276. if (start)
  277. *start = 0;
  278. if (end)
  279. *end = 0;
  280. if (flags)
  281. *flags = 0;
  282. /* TBD: allow for disabled resources */
  283. buf = skip_spaces(buf);
  284. if (start) {
  285. *start = simple_strtoull(buf, &buf, 0);
  286. if (end) {
  287. buf = skip_spaces(buf);
  288. if (*buf == '-') {
  289. buf = skip_spaces(buf + 1);
  290. *end = simple_strtoull(buf, &buf, 0);
  291. } else
  292. *end = *start;
  293. }
  294. }
  295. /* TBD: allow for additional flags, e.g., IORESOURCE_WINDOW */
  296. return buf;
  297. }
  298. static ssize_t resources_store(struct device *dmdev,
  299. struct device_attribute *attr, const char *ubuf,
  300. size_t count)
  301. {
  302. struct pnp_dev *dev = to_pnp_dev(dmdev);
  303. char *buf = (void *)ubuf;
  304. int retval = 0;
  305. if (dev->status & PNP_ATTACHED) {
  306. retval = -EBUSY;
  307. dev_info(&dev->dev, "in use; can't configure\n");
  308. goto done;
  309. }
  310. buf = skip_spaces(buf);
  311. if (!strncasecmp(buf, "disable", 7)) {
  312. retval = pnp_disable_dev(dev);
  313. goto done;
  314. }
  315. if (!strncasecmp(buf, "activate", 8)) {
  316. retval = pnp_activate_dev(dev);
  317. goto done;
  318. }
  319. if (!strncasecmp(buf, "fill", 4)) {
  320. if (dev->active)
  321. goto done;
  322. retval = pnp_auto_config_dev(dev);
  323. goto done;
  324. }
  325. if (!strncasecmp(buf, "auto", 4)) {
  326. if (dev->active)
  327. goto done;
  328. pnp_init_resources(dev);
  329. retval = pnp_auto_config_dev(dev);
  330. goto done;
  331. }
  332. if (!strncasecmp(buf, "clear", 5)) {
  333. if (dev->active)
  334. goto done;
  335. pnp_init_resources(dev);
  336. goto done;
  337. }
  338. if (!strncasecmp(buf, "get", 3)) {
  339. mutex_lock(&pnp_res_mutex);
  340. if (pnp_can_read(dev))
  341. dev->protocol->get(dev);
  342. mutex_unlock(&pnp_res_mutex);
  343. goto done;
  344. }
  345. if (!strncasecmp(buf, "set", 3)) {
  346. resource_size_t start;
  347. resource_size_t end;
  348. unsigned long flags;
  349. if (dev->active)
  350. goto done;
  351. buf += 3;
  352. pnp_init_resources(dev);
  353. mutex_lock(&pnp_res_mutex);
  354. while (1) {
  355. buf = skip_spaces(buf);
  356. if (!strncasecmp(buf, "io", 2)) {
  357. buf = pnp_get_resource_value(buf + 2,
  358. IORESOURCE_IO,
  359. &start, &end,
  360. &flags);
  361. pnp_add_io_resource(dev, start, end, flags);
  362. } else if (!strncasecmp(buf, "mem", 3)) {
  363. buf = pnp_get_resource_value(buf + 3,
  364. IORESOURCE_MEM,
  365. &start, &end,
  366. &flags);
  367. pnp_add_mem_resource(dev, start, end, flags);
  368. } else if (!strncasecmp(buf, "irq", 3)) {
  369. buf = pnp_get_resource_value(buf + 3,
  370. IORESOURCE_IRQ,
  371. &start, NULL,
  372. &flags);
  373. pnp_add_irq_resource(dev, start, flags);
  374. } else if (!strncasecmp(buf, "dma", 3)) {
  375. buf = pnp_get_resource_value(buf + 3,
  376. IORESOURCE_DMA,
  377. &start, NULL,
  378. &flags);
  379. pnp_add_dma_resource(dev, start, flags);
  380. } else if (!strncasecmp(buf, "bus", 3)) {
  381. buf = pnp_get_resource_value(buf + 3,
  382. IORESOURCE_BUS,
  383. &start, &end,
  384. NULL);
  385. pnp_add_bus_resource(dev, start, end);
  386. } else
  387. break;
  388. }
  389. mutex_unlock(&pnp_res_mutex);
  390. goto done;
  391. }
  392. done:
  393. if (retval < 0)
  394. return retval;
  395. return count;
  396. }
  397. static DEVICE_ATTR_RW(resources);
  398. static ssize_t id_show(struct device *dmdev, struct device_attribute *attr,
  399. char *buf)
  400. {
  401. char *str = buf;
  402. struct pnp_dev *dev = to_pnp_dev(dmdev);
  403. struct pnp_id *pos = dev->id;
  404. while (pos) {
  405. str += sprintf(str, "%s\n", pos->id);
  406. pos = pos->next;
  407. }
  408. return (str - buf);
  409. }
  410. static DEVICE_ATTR_RO(id);
  411. static struct attribute *pnp_dev_attrs[] = {
  412. &dev_attr_resources.attr,
  413. &dev_attr_options.attr,
  414. &dev_attr_id.attr,
  415. NULL,
  416. };
  417. static const struct attribute_group pnp_dev_group = {
  418. .attrs = pnp_dev_attrs,
  419. };
  420. const struct attribute_group *pnp_dev_groups[] = {
  421. &pnp_dev_group,
  422. NULL,
  423. };