com20020_cs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Linux ARCnet driver - COM20020 PCMCIA support
  3. *
  4. * Written 1994-1999 by Avery Pennarun,
  5. * based on an ISA version by David Woodhouse.
  6. * Derived from ibmtr_cs.c by Steve Kipisz (pcmcia-cs 3.1.4)
  7. * which was derived from pcnet_cs.c by David Hinds.
  8. * Some additional portions derived from skeleton.c by Donald Becker.
  9. *
  10. * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
  11. * for sponsoring the further development of this driver.
  12. *
  13. * **********************
  14. *
  15. * The original copyright of skeleton.c was as follows:
  16. *
  17. * skeleton.c Written 1993 by Donald Becker.
  18. * Copyright 1993 United States Government as represented by the
  19. * Director, National Security Agency. This software may only be used
  20. * and distributed according to the terms of the GNU General Public License as
  21. * modified by SRC, incorporated herein by reference.
  22. *
  23. * **********************
  24. * Changes:
  25. * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/08/2000
  26. * - reorganize kmallocs in com20020_attach, checking all for failure
  27. * and releasing the previous allocations if one fails
  28. * **********************
  29. *
  30. * For more details, see drivers/net/arcnet.c
  31. *
  32. * **********************
  33. */
  34. #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
  35. #include <linux/kernel.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include <linux/timer.h>
  40. #include <linux/delay.h>
  41. #include <linux/module.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/io.h>
  44. #include <pcmcia/cistpl.h>
  45. #include <pcmcia/ds.h>
  46. #include "arcdevice.h"
  47. #include "com20020.h"
  48. static void regdump(struct net_device *dev)
  49. {
  50. #ifdef DEBUG
  51. int ioaddr = dev->base_addr;
  52. int count;
  53. netdev_dbg(dev, "register dump:\n");
  54. for (count = 0; count < 16; count++) {
  55. if (!(count % 16))
  56. pr_cont("%04X:", ioaddr + count);
  57. pr_cont(" %02X", arcnet_inb(ioaddr, count));
  58. }
  59. pr_cont("\n");
  60. netdev_dbg(dev, "buffer0 dump:\n");
  61. /* set up the address register */
  62. count = 0;
  63. arcnet_outb((count >> 8) | RDDATAflag | AUTOINCflag,
  64. ioaddr, COM20020_REG_W_ADDR_HI);
  65. arcnet_outb(count & 0xff, ioaddr, COM20020_REG_W_ADDR_LO);
  66. for (count = 0; count < 256 + 32; count++) {
  67. if (!(count % 16))
  68. pr_cont("%04X:", count);
  69. /* copy the data */
  70. pr_cont(" %02X", arcnet_inb(ioaddr, COM20020_REG_RW_MEMDATA));
  71. }
  72. pr_cont("\n");
  73. #endif
  74. }
  75. /*====================================================================*/
  76. /* Parameters that can be set with 'insmod' */
  77. static int node;
  78. static int timeout = 3;
  79. static int backplane;
  80. static int clockp;
  81. static int clockm;
  82. module_param(node, int, 0);
  83. module_param(timeout, int, 0);
  84. module_param(backplane, int, 0);
  85. module_param(clockp, int, 0);
  86. module_param(clockm, int, 0);
  87. MODULE_DESCRIPTION("ARCnet COM20020 chipset PCMCIA driver");
  88. MODULE_LICENSE("GPL");
  89. /*====================================================================*/
  90. static int com20020_config(struct pcmcia_device *link);
  91. static void com20020_release(struct pcmcia_device *link);
  92. static void com20020_detach(struct pcmcia_device *p_dev);
  93. /*====================================================================*/
  94. static int com20020_probe(struct pcmcia_device *p_dev)
  95. {
  96. struct com20020_dev *info;
  97. struct net_device *dev;
  98. struct arcnet_local *lp;
  99. int ret = -ENOMEM;
  100. dev_dbg(&p_dev->dev, "com20020_attach()\n");
  101. /* Create new network device */
  102. info = kzalloc(sizeof(*info), GFP_KERNEL);
  103. if (!info)
  104. goto fail_alloc_info;
  105. dev = alloc_arcdev("");
  106. if (!dev)
  107. goto fail_alloc_dev;
  108. lp = netdev_priv(dev);
  109. lp->timeout = timeout;
  110. lp->backplane = backplane;
  111. lp->clockp = clockp;
  112. lp->clockm = clockm & 3;
  113. lp->hw.owner = THIS_MODULE;
  114. /* fill in our module parameters as defaults */
  115. arcnet_set_addr(dev, node);
  116. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  117. p_dev->resource[0]->end = 16;
  118. p_dev->config_flags |= CONF_ENABLE_IRQ;
  119. info->dev = dev;
  120. p_dev->priv = info;
  121. ret = com20020_config(p_dev);
  122. if (ret)
  123. goto fail_config;
  124. return 0;
  125. fail_config:
  126. free_arcdev(dev);
  127. fail_alloc_dev:
  128. kfree(info);
  129. fail_alloc_info:
  130. return ret;
  131. } /* com20020_attach */
  132. static void com20020_detach(struct pcmcia_device *link)
  133. {
  134. struct com20020_dev *info = link->priv;
  135. struct net_device *dev = info->dev;
  136. dev_dbg(&link->dev, "detach...\n");
  137. dev_dbg(&link->dev, "com20020_detach\n");
  138. dev_dbg(&link->dev, "unregister...\n");
  139. unregister_netdev(dev);
  140. /* this is necessary because we register our IRQ separately
  141. * from card services.
  142. */
  143. if (dev->irq)
  144. free_irq(dev->irq, dev);
  145. com20020_release(link);
  146. /* Unlink device structure, free bits */
  147. dev_dbg(&link->dev, "unlinking...\n");
  148. if (link->priv) {
  149. dev = info->dev;
  150. if (dev) {
  151. dev_dbg(&link->dev, "kfree...\n");
  152. free_arcdev(dev);
  153. }
  154. dev_dbg(&link->dev, "kfree2...\n");
  155. kfree(info);
  156. }
  157. } /* com20020_detach */
  158. static int com20020_config(struct pcmcia_device *link)
  159. {
  160. struct arcnet_local *lp;
  161. struct com20020_dev *info;
  162. struct net_device *dev;
  163. int i, ret;
  164. int ioaddr;
  165. info = link->priv;
  166. dev = info->dev;
  167. dev_dbg(&link->dev, "config...\n");
  168. dev_dbg(&link->dev, "com20020_config\n");
  169. dev_dbg(&link->dev, "baseport1 is %Xh\n",
  170. (unsigned int)link->resource[0]->start);
  171. i = -ENODEV;
  172. link->io_lines = 16;
  173. if (!link->resource[0]->start) {
  174. for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10) {
  175. link->resource[0]->start = ioaddr;
  176. i = pcmcia_request_io(link);
  177. if (i == 0)
  178. break;
  179. }
  180. } else {
  181. i = pcmcia_request_io(link);
  182. }
  183. if (i != 0) {
  184. dev_dbg(&link->dev, "requestIO failed totally!\n");
  185. goto failed;
  186. }
  187. ioaddr = dev->base_addr = link->resource[0]->start;
  188. dev_dbg(&link->dev, "got ioaddr %Xh\n", ioaddr);
  189. dev_dbg(&link->dev, "request IRQ %d\n",
  190. link->irq);
  191. if (!link->irq) {
  192. dev_dbg(&link->dev, "requestIRQ failed totally!\n");
  193. goto failed;
  194. }
  195. dev->irq = link->irq;
  196. ret = pcmcia_enable_device(link);
  197. if (ret)
  198. goto failed;
  199. if (com20020_check(dev)) {
  200. regdump(dev);
  201. goto failed;
  202. }
  203. lp = netdev_priv(dev);
  204. lp->card_name = "PCMCIA COM20020";
  205. lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
  206. SET_NETDEV_DEV(dev, &link->dev);
  207. i = com20020_found(dev, 0); /* calls register_netdev */
  208. if (i != 0) {
  209. dev_notice(&link->dev,
  210. "com20020_found() failed\n");
  211. goto failed;
  212. }
  213. netdev_dbg(dev, "port %#3lx, irq %d\n",
  214. dev->base_addr, dev->irq);
  215. return 0;
  216. failed:
  217. dev_dbg(&link->dev, "com20020_config failed...\n");
  218. com20020_release(link);
  219. return -ENODEV;
  220. } /* com20020_config */
  221. static void com20020_release(struct pcmcia_device *link)
  222. {
  223. dev_dbg(&link->dev, "com20020_release\n");
  224. pcmcia_disable_device(link);
  225. }
  226. static int com20020_suspend(struct pcmcia_device *link)
  227. {
  228. struct com20020_dev *info = link->priv;
  229. struct net_device *dev = info->dev;
  230. if (link->open)
  231. netif_device_detach(dev);
  232. return 0;
  233. }
  234. static int com20020_resume(struct pcmcia_device *link)
  235. {
  236. struct com20020_dev *info = link->priv;
  237. struct net_device *dev = info->dev;
  238. if (link->open) {
  239. int ioaddr = dev->base_addr;
  240. struct arcnet_local *lp = netdev_priv(dev);
  241. arcnet_outb(lp->config | 0x80, ioaddr, COM20020_REG_W_CONFIG);
  242. udelay(5);
  243. arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG);
  244. }
  245. return 0;
  246. }
  247. static const struct pcmcia_device_id com20020_ids[] = {
  248. PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.",
  249. "PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
  250. PCMCIA_DEVICE_PROD_ID12("SoHard AG",
  251. "SH ARC PCMCIA", 0xf8991729, 0x69dff0c7),
  252. PCMCIA_DEVICE_NULL
  253. };
  254. MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
  255. static struct pcmcia_driver com20020_cs_driver = {
  256. .owner = THIS_MODULE,
  257. .name = "com20020_cs",
  258. .probe = com20020_probe,
  259. .remove = com20020_detach,
  260. .id_table = com20020_ids,
  261. .suspend = com20020_suspend,
  262. .resume = com20020_resume,
  263. };
  264. module_pcmcia_driver(com20020_cs_driver);