interface.c 11 KB

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