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