interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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/stat.h>
  13. #include <linux/ctype.h>
  14. #include <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include "base.h"
  17. struct pnp_info_buffer {
  18. char *buffer; /* pointer to begin of buffer */
  19. char *curr; /* current position in buffer */
  20. unsigned long size; /* current size */
  21. unsigned long len; /* total length of buffer */
  22. int stop; /* stop flag */
  23. int error; /* error code */
  24. };
  25. typedef struct pnp_info_buffer pnp_info_buffer_t;
  26. static int pnp_printf(pnp_info_buffer_t * buffer, char *fmt, ...)
  27. {
  28. va_list args;
  29. int res;
  30. if (buffer->stop || buffer->error)
  31. return 0;
  32. va_start(args, fmt);
  33. res = vsnprintf(buffer->curr, buffer->len - buffer->size, fmt, args);
  34. va_end(args);
  35. if (buffer->size + res >= buffer->len) {
  36. buffer->stop = 1;
  37. return 0;
  38. }
  39. buffer->curr += res;
  40. buffer->size += res;
  41. return res;
  42. }
  43. static void pnp_print_port(pnp_info_buffer_t * buffer, char *space,
  44. struct pnp_port *port)
  45. {
  46. pnp_printf(buffer,
  47. "%sport 0x%x-0x%x, align 0x%x, size 0x%x, %i-bit address decoding\n",
  48. space, port->min, port->max,
  49. port->align ? (port->align - 1) : 0, port->size,
  50. port->flags & PNP_PORT_FLAG_16BITADDR ? 16 : 10);
  51. }
  52. static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
  53. struct pnp_irq *irq)
  54. {
  55. int first = 1, i;
  56. pnp_printf(buffer, "%sirq ", space);
  57. for (i = 0; i < PNP_IRQ_NR; i++)
  58. if (test_bit(i, irq->map)) {
  59. if (!first) {
  60. pnp_printf(buffer, ",");
  61. } else {
  62. first = 0;
  63. }
  64. if (i == 2 || i == 9)
  65. pnp_printf(buffer, "2/9");
  66. else
  67. pnp_printf(buffer, "%i", i);
  68. }
  69. if (bitmap_empty(irq->map, PNP_IRQ_NR))
  70. pnp_printf(buffer, "<none>");
  71. if (irq->flags & IORESOURCE_IRQ_HIGHEDGE)
  72. pnp_printf(buffer, " High-Edge");
  73. if (irq->flags & IORESOURCE_IRQ_LOWEDGE)
  74. pnp_printf(buffer, " Low-Edge");
  75. if (irq->flags & IORESOURCE_IRQ_HIGHLEVEL)
  76. pnp_printf(buffer, " High-Level");
  77. if (irq->flags & IORESOURCE_IRQ_LOWLEVEL)
  78. pnp_printf(buffer, " Low-Level");
  79. pnp_printf(buffer, "\n");
  80. }
  81. static void pnp_print_dma(pnp_info_buffer_t * buffer, char *space,
  82. struct pnp_dma *dma)
  83. {
  84. int first = 1, i;
  85. char *s;
  86. pnp_printf(buffer, "%sdma ", space);
  87. for (i = 0; i < 8; i++)
  88. if (dma->map & (1 << i)) {
  89. if (!first) {
  90. pnp_printf(buffer, ",");
  91. } else {
  92. first = 0;
  93. }
  94. pnp_printf(buffer, "%i", i);
  95. }
  96. if (!dma->map)
  97. pnp_printf(buffer, "<none>");
  98. switch (dma->flags & IORESOURCE_DMA_TYPE_MASK) {
  99. case IORESOURCE_DMA_8BIT:
  100. s = "8-bit";
  101. break;
  102. case IORESOURCE_DMA_8AND16BIT:
  103. s = "8-bit&16-bit";
  104. break;
  105. default:
  106. s = "16-bit";
  107. }
  108. pnp_printf(buffer, " %s", s);
  109. if (dma->flags & IORESOURCE_DMA_MASTER)
  110. pnp_printf(buffer, " master");
  111. if (dma->flags & IORESOURCE_DMA_BYTE)
  112. pnp_printf(buffer, " byte-count");
  113. if (dma->flags & IORESOURCE_DMA_WORD)
  114. pnp_printf(buffer, " word-count");
  115. switch (dma->flags & IORESOURCE_DMA_SPEED_MASK) {
  116. case IORESOURCE_DMA_TYPEA:
  117. s = "type-A";
  118. break;
  119. case IORESOURCE_DMA_TYPEB:
  120. s = "type-B";
  121. break;
  122. case IORESOURCE_DMA_TYPEF:
  123. s = "type-F";
  124. break;
  125. default:
  126. s = "compatible";
  127. break;
  128. }
  129. pnp_printf(buffer, " %s\n", s);
  130. }
  131. static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
  132. struct pnp_mem *mem)
  133. {
  134. char *s;
  135. pnp_printf(buffer, "%sMemory 0x%x-0x%x, align 0x%x, size 0x%x",
  136. space, mem->min, mem->max, mem->align, mem->size);
  137. if (mem->flags & IORESOURCE_MEM_WRITEABLE)
  138. pnp_printf(buffer, ", writeable");
  139. if (mem->flags & IORESOURCE_MEM_CACHEABLE)
  140. pnp_printf(buffer, ", cacheable");
  141. if (mem->flags & IORESOURCE_MEM_RANGELENGTH)
  142. pnp_printf(buffer, ", range-length");
  143. if (mem->flags & IORESOURCE_MEM_SHADOWABLE)
  144. pnp_printf(buffer, ", shadowable");
  145. if (mem->flags & IORESOURCE_MEM_EXPANSIONROM)
  146. pnp_printf(buffer, ", expansion ROM");
  147. switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
  148. case IORESOURCE_MEM_8BIT:
  149. s = "8-bit";
  150. break;
  151. case IORESOURCE_MEM_8AND16BIT:
  152. s = "8-bit&16-bit";
  153. break;
  154. case IORESOURCE_MEM_32BIT:
  155. s = "32-bit";
  156. break;
  157. default:
  158. s = "16-bit";
  159. }
  160. pnp_printf(buffer, ", %s\n", s);
  161. }
  162. static void pnp_print_option(pnp_info_buffer_t * buffer, char *space,
  163. struct pnp_option *option, int dep)
  164. {
  165. char *s;
  166. struct pnp_port *port;
  167. struct pnp_irq *irq;
  168. struct pnp_dma *dma;
  169. struct pnp_mem *mem;
  170. if (dep) {
  171. switch (option->priority) {
  172. case PNP_RES_PRIORITY_PREFERRED:
  173. s = "preferred";
  174. break;
  175. case PNP_RES_PRIORITY_ACCEPTABLE:
  176. s = "acceptable";
  177. break;
  178. case PNP_RES_PRIORITY_FUNCTIONAL:
  179. s = "functional";
  180. break;
  181. default:
  182. s = "invalid";
  183. }
  184. pnp_printf(buffer, "Dependent: %02i - Priority %s\n", dep, s);
  185. }
  186. for (port = option->port; port; port = port->next)
  187. pnp_print_port(buffer, space, port);
  188. for (irq = option->irq; irq; irq = irq->next)
  189. pnp_print_irq(buffer, space, irq);
  190. for (dma = option->dma; dma; dma = dma->next)
  191. pnp_print_dma(buffer, space, dma);
  192. for (mem = option->mem; mem; mem = mem->next)
  193. pnp_print_mem(buffer, space, mem);
  194. }
  195. static ssize_t pnp_show_options(struct device *dmdev,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct pnp_dev *dev = to_pnp_dev(dmdev);
  199. struct pnp_option *independent = dev->independent;
  200. struct pnp_option *dependent = dev->dependent;
  201. int ret, dep = 1;
  202. pnp_info_buffer_t *buffer = (pnp_info_buffer_t *)
  203. pnp_alloc(sizeof(pnp_info_buffer_t));
  204. if (!buffer)
  205. return -ENOMEM;
  206. buffer->len = PAGE_SIZE;
  207. buffer->buffer = buf;
  208. buffer->curr = buffer->buffer;
  209. if (independent)
  210. pnp_print_option(buffer, "", independent, 0);
  211. while (dependent) {
  212. pnp_print_option(buffer, " ", dependent, dep);
  213. dependent = dependent->next;
  214. dep++;
  215. }
  216. ret = (buffer->curr - buf);
  217. kfree(buffer);
  218. return ret;
  219. }
  220. static DEVICE_ATTR(options, S_IRUGO, pnp_show_options, NULL);
  221. static ssize_t pnp_show_current_resources(struct device *dmdev,
  222. struct device_attribute *attr,
  223. char *buf)
  224. {
  225. struct pnp_dev *dev = to_pnp_dev(dmdev);
  226. int i, ret;
  227. pnp_info_buffer_t *buffer;
  228. if (!dev)
  229. return -EINVAL;
  230. buffer = (pnp_info_buffer_t *) pnp_alloc(sizeof(pnp_info_buffer_t));
  231. if (!buffer)
  232. return -ENOMEM;
  233. buffer->len = PAGE_SIZE;
  234. buffer->buffer = buf;
  235. buffer->curr = buffer->buffer;
  236. pnp_printf(buffer, "state = ");
  237. if (dev->active)
  238. pnp_printf(buffer, "active\n");
  239. else
  240. pnp_printf(buffer, "disabled\n");
  241. for (i = 0; i < PNP_MAX_PORT; i++) {
  242. if (pnp_port_valid(dev, i)) {
  243. pnp_printf(buffer, "io");
  244. if (pnp_port_flags(dev, i) & IORESOURCE_DISABLED)
  245. pnp_printf(buffer, " disabled\n");
  246. else
  247. pnp_printf(buffer, " 0x%llx-0x%llx\n",
  248. (unsigned long long)
  249. pnp_port_start(dev, i),
  250. (unsigned long long)pnp_port_end(dev,
  251. i));
  252. }
  253. }
  254. for (i = 0; i < PNP_MAX_MEM; i++) {
  255. if (pnp_mem_valid(dev, i)) {
  256. pnp_printf(buffer, "mem");
  257. if (pnp_mem_flags(dev, i) & IORESOURCE_DISABLED)
  258. pnp_printf(buffer, " disabled\n");
  259. else
  260. pnp_printf(buffer, " 0x%llx-0x%llx\n",
  261. (unsigned long long)
  262. pnp_mem_start(dev, i),
  263. (unsigned long long)pnp_mem_end(dev,
  264. i));
  265. }
  266. }
  267. for (i = 0; i < PNP_MAX_IRQ; i++) {
  268. if (pnp_irq_valid(dev, i)) {
  269. pnp_printf(buffer, "irq");
  270. if (pnp_irq_flags(dev, i) & IORESOURCE_DISABLED)
  271. pnp_printf(buffer, " disabled\n");
  272. else
  273. pnp_printf(buffer, " %lld\n",
  274. (unsigned long long)pnp_irq(dev, i));
  275. }
  276. }
  277. for (i = 0; i < PNP_MAX_DMA; i++) {
  278. if (pnp_dma_valid(dev, i)) {
  279. pnp_printf(buffer, "dma");
  280. if (pnp_dma_flags(dev, i) & IORESOURCE_DISABLED)
  281. pnp_printf(buffer, " disabled\n");
  282. else
  283. pnp_printf(buffer, " %lld\n",
  284. (unsigned long long)pnp_dma(dev, i));
  285. }
  286. }
  287. ret = (buffer->curr - buf);
  288. kfree(buffer);
  289. return ret;
  290. }
  291. extern struct semaphore pnp_res_mutex;
  292. static ssize_t
  293. pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
  294. const char *ubuf, size_t count)
  295. {
  296. struct pnp_dev *dev = to_pnp_dev(dmdev);
  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_resource_table(&dev->res);
  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_resource_table(&dev->res);
  331. goto done;
  332. }
  333. if (!strnicmp(buf, "get", 3)) {
  334. down(&pnp_res_mutex);
  335. if (pnp_can_read(dev))
  336. dev->protocol->get(dev, &dev->res);
  337. up(&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_resource_table(&dev->res);
  346. down(&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. dev->res.port_resource[nport].start =
  355. simple_strtoul(buf, &buf, 0);
  356. while (isspace(*buf))
  357. ++buf;
  358. if (*buf == '-') {
  359. buf += 1;
  360. while (isspace(*buf))
  361. ++buf;
  362. dev->res.port_resource[nport].end =
  363. simple_strtoul(buf, &buf, 0);
  364. } else
  365. dev->res.port_resource[nport].end =
  366. dev->res.port_resource[nport].start;
  367. dev->res.port_resource[nport].flags =
  368. IORESOURCE_IO;
  369. nport++;
  370. if (nport >= PNP_MAX_PORT)
  371. break;
  372. continue;
  373. }
  374. if (!strnicmp(buf, "mem", 3)) {
  375. buf += 3;
  376. while (isspace(*buf))
  377. ++buf;
  378. dev->res.mem_resource[nmem].start =
  379. simple_strtoul(buf, &buf, 0);
  380. while (isspace(*buf))
  381. ++buf;
  382. if (*buf == '-') {
  383. buf += 1;
  384. while (isspace(*buf))
  385. ++buf;
  386. dev->res.mem_resource[nmem].end =
  387. simple_strtoul(buf, &buf, 0);
  388. } else
  389. dev->res.mem_resource[nmem].end =
  390. dev->res.mem_resource[nmem].start;
  391. dev->res.mem_resource[nmem].flags =
  392. IORESOURCE_MEM;
  393. nmem++;
  394. if (nmem >= PNP_MAX_MEM)
  395. break;
  396. continue;
  397. }
  398. if (!strnicmp(buf, "irq", 3)) {
  399. buf += 3;
  400. while (isspace(*buf))
  401. ++buf;
  402. dev->res.irq_resource[nirq].start =
  403. dev->res.irq_resource[nirq].end =
  404. simple_strtoul(buf, &buf, 0);
  405. dev->res.irq_resource[nirq].flags =
  406. IORESOURCE_IRQ;
  407. nirq++;
  408. if (nirq >= PNP_MAX_IRQ)
  409. break;
  410. continue;
  411. }
  412. if (!strnicmp(buf, "dma", 3)) {
  413. buf += 3;
  414. while (isspace(*buf))
  415. ++buf;
  416. dev->res.dma_resource[ndma].start =
  417. dev->res.dma_resource[ndma].end =
  418. simple_strtoul(buf, &buf, 0);
  419. dev->res.dma_resource[ndma].flags =
  420. IORESOURCE_DMA;
  421. ndma++;
  422. if (ndma >= PNP_MAX_DMA)
  423. break;
  424. continue;
  425. }
  426. break;
  427. }
  428. up(&pnp_res_mutex);
  429. goto done;
  430. }
  431. done:
  432. if (retval < 0)
  433. return retval;
  434. return count;
  435. }
  436. static DEVICE_ATTR(resources, S_IRUGO | S_IWUSR,
  437. pnp_show_current_resources, pnp_set_current_resources);
  438. static ssize_t pnp_show_current_ids(struct device *dmdev,
  439. struct device_attribute *attr, char *buf)
  440. {
  441. char *str = buf;
  442. struct pnp_dev *dev = to_pnp_dev(dmdev);
  443. struct pnp_id *pos = dev->id;
  444. while (pos) {
  445. str += sprintf(str, "%s\n", pos->id);
  446. pos = pos->next;
  447. }
  448. return (str - buf);
  449. }
  450. static DEVICE_ATTR(id, S_IRUGO, pnp_show_current_ids, NULL);
  451. int pnp_interface_attach_device(struct pnp_dev *dev)
  452. {
  453. int rc = device_create_file(&dev->dev, &dev_attr_options);
  454. if (rc)
  455. goto err;
  456. rc = device_create_file(&dev->dev, &dev_attr_resources);
  457. if (rc)
  458. goto err_opt;
  459. rc = device_create_file(&dev->dev, &dev_attr_id);
  460. if (rc)
  461. goto err_res;
  462. return 0;
  463. err_res:
  464. device_remove_file(&dev->dev, &dev_attr_resources);
  465. err_opt:
  466. device_remove_file(&dev->dev, &dev_attr_options);
  467. err:
  468. return rc;
  469. }