interface.c 11 KB

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