interface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/pnp.h>
  15. #include <linux/stat.h>
  16. #include <linux/ctype.h>
  17. #include <linux/slab.h>
  18. #include <linux/mutex.h>
  19. #include <asm/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 pnp_show_options(struct device *dmdev,
  192. struct device_attribute *attr, 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(options, S_IRUGO, pnp_show_options, NULL);
  226. static ssize_t pnp_show_current_resources(struct device *dmdev,
  227. struct device_attribute *attr,
  228. 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 = pnp_alloc(sizeof(pnp_info_buffer_t));
  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. pnp_printf(buffer, " %#llx-%#llx\n",
  255. (unsigned long long) res->start,
  256. (unsigned long long) res->end);
  257. break;
  258. case IORESOURCE_IRQ:
  259. case IORESOURCE_DMA:
  260. pnp_printf(buffer, " %lld\n",
  261. (unsigned long long) res->start);
  262. break;
  263. }
  264. }
  265. ret = (buffer->curr - buf);
  266. kfree(buffer);
  267. return ret;
  268. }
  269. static ssize_t pnp_set_current_resources(struct device *dmdev,
  270. struct device_attribute *attr,
  271. const char *ubuf, size_t count)
  272. {
  273. struct pnp_dev *dev = to_pnp_dev(dmdev);
  274. char *buf = (void *)ubuf;
  275. int retval = 0;
  276. resource_size_t start, end;
  277. if (dev->status & PNP_ATTACHED) {
  278. retval = -EBUSY;
  279. dev_info(&dev->dev, "in use; can't configure\n");
  280. goto done;
  281. }
  282. while (isspace(*buf))
  283. ++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. while (isspace(*buf))
  326. ++buf;
  327. if (!strnicmp(buf, "io", 2)) {
  328. buf += 2;
  329. while (isspace(*buf))
  330. ++buf;
  331. start = simple_strtoul(buf, &buf, 0);
  332. while (isspace(*buf))
  333. ++buf;
  334. if (*buf == '-') {
  335. buf += 1;
  336. while (isspace(*buf))
  337. ++buf;
  338. end = simple_strtoul(buf, &buf, 0);
  339. } else
  340. end = start;
  341. pnp_add_io_resource(dev, start, end, 0);
  342. continue;
  343. }
  344. if (!strnicmp(buf, "mem", 3)) {
  345. buf += 3;
  346. while (isspace(*buf))
  347. ++buf;
  348. start = simple_strtoul(buf, &buf, 0);
  349. while (isspace(*buf))
  350. ++buf;
  351. if (*buf == '-') {
  352. buf += 1;
  353. while (isspace(*buf))
  354. ++buf;
  355. end = simple_strtoul(buf, &buf, 0);
  356. } else
  357. end = start;
  358. pnp_add_mem_resource(dev, start, end, 0);
  359. continue;
  360. }
  361. if (!strnicmp(buf, "irq", 3)) {
  362. buf += 3;
  363. while (isspace(*buf))
  364. ++buf;
  365. start = simple_strtoul(buf, &buf, 0);
  366. pnp_add_irq_resource(dev, start, 0);
  367. continue;
  368. }
  369. if (!strnicmp(buf, "dma", 3)) {
  370. buf += 3;
  371. while (isspace(*buf))
  372. ++buf;
  373. start = simple_strtoul(buf, &buf, 0);
  374. pnp_add_dma_resource(dev, start, 0);
  375. continue;
  376. }
  377. break;
  378. }
  379. mutex_unlock(&pnp_res_mutex);
  380. goto done;
  381. }
  382. done:
  383. if (retval < 0)
  384. return retval;
  385. return count;
  386. }
  387. static DEVICE_ATTR(resources, S_IRUGO | S_IWUSR,
  388. pnp_show_current_resources, pnp_set_current_resources);
  389. static ssize_t pnp_show_current_ids(struct device *dmdev,
  390. struct device_attribute *attr, char *buf)
  391. {
  392. char *str = buf;
  393. struct pnp_dev *dev = to_pnp_dev(dmdev);
  394. struct pnp_id *pos = dev->id;
  395. while (pos) {
  396. str += sprintf(str, "%s\n", pos->id);
  397. pos = pos->next;
  398. }
  399. return (str - buf);
  400. }
  401. static DEVICE_ATTR(id, S_IRUGO, pnp_show_current_ids, NULL);
  402. int pnp_interface_attach_device(struct pnp_dev *dev)
  403. {
  404. int rc = device_create_file(&dev->dev, &dev_attr_options);
  405. if (rc)
  406. goto err;
  407. rc = device_create_file(&dev->dev, &dev_attr_resources);
  408. if (rc)
  409. goto err_opt;
  410. rc = device_create_file(&dev->dev, &dev_attr_id);
  411. if (rc)
  412. goto err_res;
  413. return 0;
  414. err_res:
  415. device_remove_file(&dev->dev, &dev_attr_resources);
  416. err_opt:
  417. device_remove_file(&dev->dev, &dev_attr_options);
  418. err:
  419. return rc;
  420. }