interface.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 pnp_show_options(struct device *dmdev,
  191. struct device_attribute *attr, 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 ssize_t pnp_show_current_resources(struct device *dmdev,
  225. struct device_attribute *attr,
  226. 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 ssize_t pnp_set_current_resources(struct device *dmdev,
  271. struct device_attribute *attr,
  272. const char *ubuf, size_t count)
  273. {
  274. struct pnp_dev *dev = to_pnp_dev(dmdev);
  275. char *buf = (void *)ubuf;
  276. int retval = 0;
  277. resource_size_t start, end;
  278. if (dev->status & PNP_ATTACHED) {
  279. retval = -EBUSY;
  280. dev_info(&dev->dev, "in use; can't configure\n");
  281. goto done;
  282. }
  283. buf = skip_spaces(buf);
  284. if (!strnicmp(buf, "disable", 7)) {
  285. retval = pnp_disable_dev(dev);
  286. goto done;
  287. }
  288. if (!strnicmp(buf, "activate", 8)) {
  289. retval = pnp_activate_dev(dev);
  290. goto done;
  291. }
  292. if (!strnicmp(buf, "fill", 4)) {
  293. if (dev->active)
  294. goto done;
  295. retval = pnp_auto_config_dev(dev);
  296. goto done;
  297. }
  298. if (!strnicmp(buf, "auto", 4)) {
  299. if (dev->active)
  300. goto done;
  301. pnp_init_resources(dev);
  302. retval = pnp_auto_config_dev(dev);
  303. goto done;
  304. }
  305. if (!strnicmp(buf, "clear", 5)) {
  306. if (dev->active)
  307. goto done;
  308. pnp_init_resources(dev);
  309. goto done;
  310. }
  311. if (!strnicmp(buf, "get", 3)) {
  312. mutex_lock(&pnp_res_mutex);
  313. if (pnp_can_read(dev))
  314. dev->protocol->get(dev);
  315. mutex_unlock(&pnp_res_mutex);
  316. goto done;
  317. }
  318. if (!strnicmp(buf, "set", 3)) {
  319. if (dev->active)
  320. goto done;
  321. buf += 3;
  322. pnp_init_resources(dev);
  323. mutex_lock(&pnp_res_mutex);
  324. while (1) {
  325. buf = skip_spaces(buf);
  326. if (!strnicmp(buf, "io", 2)) {
  327. buf = skip_spaces(buf + 2);
  328. start = simple_strtoul(buf, &buf, 0);
  329. buf = skip_spaces(buf);
  330. if (*buf == '-') {
  331. buf = skip_spaces(buf + 1);
  332. end = simple_strtoul(buf, &buf, 0);
  333. } else
  334. end = start;
  335. pnp_add_io_resource(dev, start, end, 0);
  336. continue;
  337. }
  338. if (!strnicmp(buf, "mem", 3)) {
  339. buf = skip_spaces(buf + 3);
  340. start = simple_strtoul(buf, &buf, 0);
  341. buf = skip_spaces(buf);
  342. if (*buf == '-') {
  343. buf = skip_spaces(buf + 1);
  344. end = simple_strtoul(buf, &buf, 0);
  345. } else
  346. end = start;
  347. pnp_add_mem_resource(dev, start, end, 0);
  348. continue;
  349. }
  350. if (!strnicmp(buf, "irq", 3)) {
  351. buf = skip_spaces(buf + 3);
  352. start = simple_strtoul(buf, &buf, 0);
  353. pnp_add_irq_resource(dev, start, 0);
  354. continue;
  355. }
  356. if (!strnicmp(buf, "dma", 3)) {
  357. buf = skip_spaces(buf + 3);
  358. start = simple_strtoul(buf, &buf, 0);
  359. pnp_add_dma_resource(dev, start, 0);
  360. continue;
  361. }
  362. break;
  363. }
  364. mutex_unlock(&pnp_res_mutex);
  365. goto done;
  366. }
  367. done:
  368. if (retval < 0)
  369. return retval;
  370. return count;
  371. }
  372. static ssize_t pnp_show_current_ids(struct device *dmdev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. char *str = buf;
  376. struct pnp_dev *dev = to_pnp_dev(dmdev);
  377. struct pnp_id *pos = dev->id;
  378. while (pos) {
  379. str += sprintf(str, "%s\n", pos->id);
  380. pos = pos->next;
  381. }
  382. return (str - buf);
  383. }
  384. struct device_attribute pnp_interface_attrs[] = {
  385. __ATTR(resources, S_IRUGO | S_IWUSR,
  386. pnp_show_current_resources,
  387. pnp_set_current_resources),
  388. __ATTR(options, S_IRUGO, pnp_show_options, NULL),
  389. __ATTR(id, S_IRUGO, pnp_show_current_ids, NULL),
  390. __ATTR_NULL,
  391. };