interface.c 11 KB

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