interface.c 11 KB

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