interface.c 11 KB

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