interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. if (dev->status & PNP_ATTACHED) {
  300. retval = -EBUSY;
  301. dev_info(&dev->dev, "in use; can't configure\n");
  302. goto done;
  303. }
  304. while (isspace(*buf))
  305. ++buf;
  306. if (!strnicmp(buf, "disable", 7)) {
  307. retval = pnp_disable_dev(dev);
  308. goto done;
  309. }
  310. if (!strnicmp(buf, "activate", 8)) {
  311. retval = pnp_activate_dev(dev);
  312. goto done;
  313. }
  314. if (!strnicmp(buf, "fill", 4)) {
  315. if (dev->active)
  316. goto done;
  317. retval = pnp_auto_config_dev(dev);
  318. goto done;
  319. }
  320. if (!strnicmp(buf, "auto", 4)) {
  321. if (dev->active)
  322. goto done;
  323. pnp_init_resources(dev);
  324. retval = pnp_auto_config_dev(dev);
  325. goto done;
  326. }
  327. if (!strnicmp(buf, "clear", 5)) {
  328. if (dev->active)
  329. goto done;
  330. pnp_init_resources(dev);
  331. goto done;
  332. }
  333. if (!strnicmp(buf, "get", 3)) {
  334. mutex_lock(&pnp_res_mutex);
  335. if (pnp_can_read(dev))
  336. dev->protocol->get(dev);
  337. mutex_unlock(&pnp_res_mutex);
  338. goto done;
  339. }
  340. if (!strnicmp(buf, "set", 3)) {
  341. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  342. if (dev->active)
  343. goto done;
  344. buf += 3;
  345. pnp_init_resources(dev);
  346. mutex_lock(&pnp_res_mutex);
  347. while (1) {
  348. while (isspace(*buf))
  349. ++buf;
  350. if (!strnicmp(buf, "io", 2)) {
  351. buf += 2;
  352. while (isspace(*buf))
  353. ++buf;
  354. pnp_res = pnp_get_pnp_resource(dev,
  355. IORESOURCE_IO, nport);
  356. if (!pnp_res)
  357. break;
  358. pnp_res->index = nport;
  359. res = &pnp_res->res;
  360. res->start = simple_strtoul(buf, &buf, 0);
  361. while (isspace(*buf))
  362. ++buf;
  363. if (*buf == '-') {
  364. buf += 1;
  365. while (isspace(*buf))
  366. ++buf;
  367. res->end = simple_strtoul(buf, &buf, 0);
  368. } else
  369. res->end = res->start;
  370. res->flags = IORESOURCE_IO;
  371. nport++;
  372. continue;
  373. }
  374. if (!strnicmp(buf, "mem", 3)) {
  375. buf += 3;
  376. while (isspace(*buf))
  377. ++buf;
  378. pnp_res = pnp_get_pnp_resource(dev,
  379. IORESOURCE_MEM, nmem);
  380. if (!pnp_res)
  381. break;
  382. pnp_res->index = nmem;
  383. res = &pnp_res->res;
  384. res->start = simple_strtoul(buf, &buf, 0);
  385. while (isspace(*buf))
  386. ++buf;
  387. if (*buf == '-') {
  388. buf += 1;
  389. while (isspace(*buf))
  390. ++buf;
  391. res->end = simple_strtoul(buf, &buf, 0);
  392. } else
  393. res->end = res->start;
  394. res->flags = IORESOURCE_MEM;
  395. nmem++;
  396. continue;
  397. }
  398. if (!strnicmp(buf, "irq", 3)) {
  399. buf += 3;
  400. while (isspace(*buf))
  401. ++buf;
  402. pnp_res = pnp_get_pnp_resource(dev,
  403. IORESOURCE_IRQ, nirq);
  404. if (!pnp_res)
  405. break;
  406. pnp_res->index = nirq;
  407. res = &pnp_res->res;
  408. res->start = res->end =
  409. simple_strtoul(buf, &buf, 0);
  410. res->flags = IORESOURCE_IRQ;
  411. nirq++;
  412. continue;
  413. }
  414. if (!strnicmp(buf, "dma", 3)) {
  415. buf += 3;
  416. while (isspace(*buf))
  417. ++buf;
  418. pnp_res = pnp_get_pnp_resource(dev,
  419. IORESOURCE_DMA, ndma);
  420. if (!pnp_res)
  421. break;
  422. pnp_res->index = ndma;
  423. res = &pnp_res->res;
  424. res->start = res->end =
  425. simple_strtoul(buf, &buf, 0);
  426. res->flags = IORESOURCE_DMA;
  427. ndma++;
  428. continue;
  429. }
  430. break;
  431. }
  432. mutex_unlock(&pnp_res_mutex);
  433. goto done;
  434. }
  435. done:
  436. if (retval < 0)
  437. return retval;
  438. return count;
  439. }
  440. static DEVICE_ATTR(resources, S_IRUGO | S_IWUSR,
  441. pnp_show_current_resources, pnp_set_current_resources);
  442. static ssize_t pnp_show_current_ids(struct device *dmdev,
  443. struct device_attribute *attr, char *buf)
  444. {
  445. char *str = buf;
  446. struct pnp_dev *dev = to_pnp_dev(dmdev);
  447. struct pnp_id *pos = dev->id;
  448. while (pos) {
  449. str += sprintf(str, "%s\n", pos->id);
  450. pos = pos->next;
  451. }
  452. return (str - buf);
  453. }
  454. static DEVICE_ATTR(id, S_IRUGO, pnp_show_current_ids, NULL);
  455. int pnp_interface_attach_device(struct pnp_dev *dev)
  456. {
  457. int rc = device_create_file(&dev->dev, &dev_attr_options);
  458. if (rc)
  459. goto err;
  460. rc = device_create_file(&dev->dev, &dev_attr_resources);
  461. if (rc)
  462. goto err_opt;
  463. rc = device_create_file(&dev->dev, &dev_attr_id);
  464. if (rc)
  465. goto err_res;
  466. return 0;
  467. err_res:
  468. device_remove_file(&dev->dev, &dev_attr_resources);
  469. err_opt:
  470. device_remove_file(&dev->dev, &dev_attr_options);
  471. err:
  472. return rc;
  473. }