interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. struct pnp_option *independent = dev->independent;
  202. struct pnp_option *dependent = dev->dependent;
  203. int ret, dep = 1;
  204. pnp_info_buffer_t *buffer = (pnp_info_buffer_t *)
  205. 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. struct resource *res;
  229. int i, ret;
  230. pnp_info_buffer_t *buffer;
  231. if (!dev)
  232. return -EINVAL;
  233. buffer = (pnp_info_buffer_t *) pnp_alloc(sizeof(pnp_info_buffer_t));
  234. if (!buffer)
  235. return -ENOMEM;
  236. buffer->len = PAGE_SIZE;
  237. buffer->buffer = buf;
  238. buffer->curr = buffer->buffer;
  239. pnp_printf(buffer, "state = ");
  240. if (dev->active)
  241. pnp_printf(buffer, "active\n");
  242. else
  243. pnp_printf(buffer, "disabled\n");
  244. for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
  245. if (pnp_resource_valid(res)) {
  246. pnp_printf(buffer, "io");
  247. if (res->flags & IORESOURCE_DISABLED)
  248. pnp_printf(buffer, " disabled\n");
  249. else
  250. pnp_printf(buffer, " 0x%llx-0x%llx\n",
  251. (unsigned long long) res->start,
  252. (unsigned long long) res->end);
  253. }
  254. }
  255. for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
  256. if (pnp_resource_valid(res)) {
  257. pnp_printf(buffer, "mem");
  258. if (res->flags & IORESOURCE_DISABLED)
  259. pnp_printf(buffer, " disabled\n");
  260. else
  261. pnp_printf(buffer, " 0x%llx-0x%llx\n",
  262. (unsigned long long) res->start,
  263. (unsigned long long) res->end);
  264. }
  265. }
  266. for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
  267. if (pnp_resource_valid(res)) {
  268. pnp_printf(buffer, "irq");
  269. if (res->flags & IORESOURCE_DISABLED)
  270. pnp_printf(buffer, " disabled\n");
  271. else
  272. pnp_printf(buffer, " %lld\n",
  273. (unsigned long long) res->start);
  274. }
  275. }
  276. for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) {
  277. if (pnp_resource_valid(res)) {
  278. pnp_printf(buffer, "dma");
  279. if (res->flags & IORESOURCE_DISABLED)
  280. pnp_printf(buffer, " disabled\n");
  281. else
  282. pnp_printf(buffer, " %lld\n",
  283. (unsigned long long) res->start);
  284. }
  285. }
  286. ret = (buffer->curr - buf);
  287. kfree(buffer);
  288. return ret;
  289. }
  290. static ssize_t
  291. pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
  292. const char *ubuf, size_t count)
  293. {
  294. struct pnp_dev *dev = to_pnp_dev(dmdev);
  295. struct pnp_resource *pnp_res;
  296. struct resource *res;
  297. char *buf = (void *)ubuf;
  298. int retval = 0;
  299. resource_size_t start;
  300. if (dev->status & PNP_ATTACHED) {
  301. retval = -EBUSY;
  302. dev_info(&dev->dev, "in use; can't configure\n");
  303. goto done;
  304. }
  305. while (isspace(*buf))
  306. ++buf;
  307. if (!strnicmp(buf, "disable", 7)) {
  308. retval = pnp_disable_dev(dev);
  309. goto done;
  310. }
  311. if (!strnicmp(buf, "activate", 8)) {
  312. retval = pnp_activate_dev(dev);
  313. goto done;
  314. }
  315. if (!strnicmp(buf, "fill", 4)) {
  316. if (dev->active)
  317. goto done;
  318. retval = pnp_auto_config_dev(dev);
  319. goto done;
  320. }
  321. if (!strnicmp(buf, "auto", 4)) {
  322. if (dev->active)
  323. goto done;
  324. pnp_init_resources(dev);
  325. retval = pnp_auto_config_dev(dev);
  326. goto done;
  327. }
  328. if (!strnicmp(buf, "clear", 5)) {
  329. if (dev->active)
  330. goto done;
  331. pnp_init_resources(dev);
  332. goto done;
  333. }
  334. if (!strnicmp(buf, "get", 3)) {
  335. mutex_lock(&pnp_res_mutex);
  336. if (pnp_can_read(dev))
  337. dev->protocol->get(dev);
  338. mutex_unlock(&pnp_res_mutex);
  339. goto done;
  340. }
  341. if (!strnicmp(buf, "set", 3)) {
  342. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  343. if (dev->active)
  344. goto done;
  345. buf += 3;
  346. pnp_init_resources(dev);
  347. mutex_lock(&pnp_res_mutex);
  348. while (1) {
  349. while (isspace(*buf))
  350. ++buf;
  351. if (!strnicmp(buf, "io", 2)) {
  352. buf += 2;
  353. while (isspace(*buf))
  354. ++buf;
  355. pnp_res = pnp_get_pnp_resource(dev,
  356. IORESOURCE_IO, nport);
  357. if (!pnp_res)
  358. break;
  359. pnp_res->index = nport;
  360. res = &pnp_res->res;
  361. res->start = simple_strtoul(buf, &buf, 0);
  362. while (isspace(*buf))
  363. ++buf;
  364. if (*buf == '-') {
  365. buf += 1;
  366. while (isspace(*buf))
  367. ++buf;
  368. res->end = simple_strtoul(buf, &buf, 0);
  369. } else
  370. res->end = res->start;
  371. res->flags = IORESOURCE_IO;
  372. nport++;
  373. continue;
  374. }
  375. if (!strnicmp(buf, "mem", 3)) {
  376. buf += 3;
  377. while (isspace(*buf))
  378. ++buf;
  379. pnp_res = pnp_get_pnp_resource(dev,
  380. IORESOURCE_MEM, nmem);
  381. if (!pnp_res)
  382. break;
  383. pnp_res->index = nmem;
  384. res = &pnp_res->res;
  385. res->start = simple_strtoul(buf, &buf, 0);
  386. while (isspace(*buf))
  387. ++buf;
  388. if (*buf == '-') {
  389. buf += 1;
  390. while (isspace(*buf))
  391. ++buf;
  392. res->end = simple_strtoul(buf, &buf, 0);
  393. } else
  394. res->end = res->start;
  395. res->flags = IORESOURCE_MEM;
  396. nmem++;
  397. continue;
  398. }
  399. if (!strnicmp(buf, "irq", 3)) {
  400. buf += 3;
  401. while (isspace(*buf))
  402. ++buf;
  403. start = simple_strtoul(buf, &buf, 0);
  404. pnp_res = pnp_add_irq_resource(dev, start, 0);
  405. if (pnp_res)
  406. nirq++;
  407. continue;
  408. }
  409. if (!strnicmp(buf, "dma", 3)) {
  410. buf += 3;
  411. while (isspace(*buf))
  412. ++buf;
  413. start = simple_strtoul(buf, &buf, 0);
  414. pnp_res = pnp_add_dma_resource(dev, start, 0);
  415. if (pnp_res)
  416. pnp_res->index = ndma++;
  417. continue;
  418. }
  419. break;
  420. }
  421. mutex_unlock(&pnp_res_mutex);
  422. goto done;
  423. }
  424. done:
  425. if (retval < 0)
  426. return retval;
  427. return count;
  428. }
  429. static DEVICE_ATTR(resources, S_IRUGO | S_IWUSR,
  430. pnp_show_current_resources, pnp_set_current_resources);
  431. static ssize_t pnp_show_current_ids(struct device *dmdev,
  432. struct device_attribute *attr, char *buf)
  433. {
  434. char *str = buf;
  435. struct pnp_dev *dev = to_pnp_dev(dmdev);
  436. struct pnp_id *pos = dev->id;
  437. while (pos) {
  438. str += sprintf(str, "%s\n", pos->id);
  439. pos = pos->next;
  440. }
  441. return (str - buf);
  442. }
  443. static DEVICE_ATTR(id, S_IRUGO, pnp_show_current_ids, NULL);
  444. int pnp_interface_attach_device(struct pnp_dev *dev)
  445. {
  446. int rc = device_create_file(&dev->dev, &dev_attr_options);
  447. if (rc)
  448. goto err;
  449. rc = device_create_file(&dev->dev, &dev_attr_resources);
  450. if (rc)
  451. goto err_opt;
  452. rc = device_create_file(&dev->dev, &dev_attr_id);
  453. if (rc)
  454. goto err_res;
  455. return 0;
  456. err_res:
  457. device_remove_file(&dev->dev, &dev_attr_resources);
  458. err_opt:
  459. device_remove_file(&dev->dev, &dev_attr_options);
  460. err:
  461. return rc;
  462. }