com20020_cs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #include <linux/kernel.h>
  35. #include <linux/init.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/arcdevice.h>
  44. #include <linux/com20020.h>
  45. #include <pcmcia/cistpl.h>
  46. #include <pcmcia/ds.h>
  47. #include <asm/io.h>
  48. #include <asm/system.h>
  49. #define VERSION "arcnet: COM20020 PCMCIA support loaded.\n"
  50. static void regdump(struct net_device *dev)
  51. {
  52. #ifdef DEBUG
  53. int ioaddr = dev->base_addr;
  54. int count;
  55. netdev_dbg(dev, "register dump:\n");
  56. for (count = ioaddr; count < ioaddr + 16; count++)
  57. {
  58. if (!(count % 16))
  59. pr_cont("%04X:", count);
  60. pr_cont(" %02X", inb(count));
  61. }
  62. pr_cont("\n");
  63. netdev_dbg(dev, "buffer0 dump:\n");
  64. /* set up the address register */
  65. count = 0;
  66. outb((count >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
  67. outb(count & 0xff, _ADDR_LO);
  68. for (count = 0; count < 256+32; count++)
  69. {
  70. if (!(count % 16))
  71. pr_cont("%04X:", count);
  72. /* copy the data */
  73. pr_cont(" %02X", inb(_MEMDATA));
  74. }
  75. pr_cont("\n");
  76. #endif
  77. }
  78. /*====================================================================*/
  79. /* Parameters that can be set with 'insmod' */
  80. static int node;
  81. static int timeout = 3;
  82. static int backplane;
  83. static int clockp;
  84. static int clockm;
  85. module_param(node, int, 0);
  86. module_param(timeout, int, 0);
  87. module_param(backplane, int, 0);
  88. module_param(clockp, int, 0);
  89. module_param(clockm, int, 0);
  90. MODULE_LICENSE("GPL");
  91. /*====================================================================*/
  92. static int com20020_config(struct pcmcia_device *link);
  93. static void com20020_release(struct pcmcia_device *link);
  94. static void com20020_detach(struct pcmcia_device *p_dev);
  95. /*====================================================================*/
  96. typedef struct com20020_dev_t {
  97. struct net_device *dev;
  98. } com20020_dev_t;
  99. static int com20020_probe(struct pcmcia_device *p_dev)
  100. {
  101. com20020_dev_t *info;
  102. struct net_device *dev;
  103. struct arcnet_local *lp;
  104. dev_dbg(&p_dev->dev, "com20020_attach()\n");
  105. /* Create new network device */
  106. info = kzalloc(sizeof(struct com20020_dev_t), GFP_KERNEL);
  107. if (!info)
  108. goto fail_alloc_info;
  109. dev = alloc_arcdev("");
  110. if (!dev)
  111. goto fail_alloc_dev;
  112. lp = netdev_priv(dev);
  113. lp->timeout = timeout;
  114. lp->backplane = backplane;
  115. lp->clockp = clockp;
  116. lp->clockm = clockm & 3;
  117. lp->hw.owner = THIS_MODULE;
  118. /* fill in our module parameters as defaults */
  119. dev->dev_addr[0] = node;
  120. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  121. p_dev->resource[0]->end = 16;
  122. p_dev->config_flags |= CONF_ENABLE_IRQ;
  123. info->dev = dev;
  124. p_dev->priv = info;
  125. return com20020_config(p_dev);
  126. fail_alloc_dev:
  127. kfree(info);
  128. fail_alloc_info:
  129. return -ENOMEM;
  130. } /* com20020_attach */
  131. static void com20020_detach(struct pcmcia_device *link)
  132. {
  133. struct com20020_dev_t *info = link->priv;
  134. struct net_device *dev = info->dev;
  135. dev_dbg(&link->dev, "detach...\n");
  136. dev_dbg(&link->dev, "com20020_detach\n");
  137. dev_dbg(&link->dev, "unregister...\n");
  138. unregister_netdev(dev);
  139. /*
  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. {
  150. dev = info->dev;
  151. if (dev)
  152. {
  153. dev_dbg(&link->dev, "kfree...\n");
  154. free_netdev(dev);
  155. }
  156. dev_dbg(&link->dev, "kfree2...\n");
  157. kfree(info);
  158. }
  159. } /* com20020_detach */
  160. static int com20020_config(struct pcmcia_device *link)
  161. {
  162. struct arcnet_local *lp;
  163. com20020_dev_t *info;
  164. struct net_device *dev;
  165. int i, ret;
  166. int ioaddr;
  167. info = link->priv;
  168. dev = info->dev;
  169. dev_dbg(&link->dev, "config...\n");
  170. dev_dbg(&link->dev, "com20020_config\n");
  171. dev_dbg(&link->dev, "baseport1 is %Xh\n",
  172. (unsigned int) link->resource[0]->start);
  173. i = -ENODEV;
  174. link->io_lines = 16;
  175. if (!link->resource[0]->start)
  176. {
  177. for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10)
  178. {
  179. link->resource[0]->start = ioaddr;
  180. i = pcmcia_request_io(link);
  181. if (i == 0)
  182. break;
  183. }
  184. }
  185. else
  186. i = pcmcia_request_io(link);
  187. if (i != 0)
  188. {
  189. dev_dbg(&link->dev, "requestIO failed totally!\n");
  190. goto failed;
  191. }
  192. ioaddr = dev->base_addr = link->resource[0]->start;
  193. dev_dbg(&link->dev, "got ioaddr %Xh\n", ioaddr);
  194. dev_dbg(&link->dev, "request IRQ %d\n",
  195. link->irq);
  196. if (!link->irq)
  197. {
  198. dev_dbg(&link->dev, "requestIRQ failed totally!\n");
  199. goto failed;
  200. }
  201. dev->irq = link->irq;
  202. ret = pcmcia_enable_device(link);
  203. if (ret)
  204. goto failed;
  205. if (com20020_check(dev))
  206. {
  207. regdump(dev);
  208. goto failed;
  209. }
  210. lp = netdev_priv(dev);
  211. lp->card_name = "PCMCIA COM20020";
  212. lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
  213. SET_NETDEV_DEV(dev, &link->dev);
  214. i = com20020_found(dev, 0); /* calls register_netdev */
  215. if (i != 0) {
  216. dev_notice(&link->dev,
  217. "com20020_found() failed\n");
  218. goto failed;
  219. }
  220. netdev_dbg(dev, "port %#3lx, irq %d\n",
  221. dev->base_addr, dev->irq);
  222. return 0;
  223. failed:
  224. dev_dbg(&link->dev, "com20020_config failed...\n");
  225. com20020_release(link);
  226. return -ENODEV;
  227. } /* com20020_config */
  228. static void com20020_release(struct pcmcia_device *link)
  229. {
  230. dev_dbg(&link->dev, "com20020_release\n");
  231. pcmcia_disable_device(link);
  232. }
  233. static int com20020_suspend(struct pcmcia_device *link)
  234. {
  235. com20020_dev_t *info = link->priv;
  236. struct net_device *dev = info->dev;
  237. if (link->open)
  238. netif_device_detach(dev);
  239. return 0;
  240. }
  241. static int com20020_resume(struct pcmcia_device *link)
  242. {
  243. com20020_dev_t *info = link->priv;
  244. struct net_device *dev = info->dev;
  245. if (link->open) {
  246. int ioaddr = dev->base_addr;
  247. struct arcnet_local *lp = netdev_priv(dev);
  248. ARCRESET;
  249. }
  250. return 0;
  251. }
  252. static const struct pcmcia_device_id com20020_ids[] = {
  253. PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.",
  254. "PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
  255. PCMCIA_DEVICE_PROD_ID12("SoHard AG",
  256. "SH ARC PCMCIA", 0xf8991729, 0x69dff0c7),
  257. PCMCIA_DEVICE_NULL
  258. };
  259. MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
  260. static struct pcmcia_driver com20020_cs_driver = {
  261. .owner = THIS_MODULE,
  262. .name = "com20020_cs",
  263. .probe = com20020_probe,
  264. .remove = com20020_detach,
  265. .id_table = com20020_ids,
  266. .suspend = com20020_suspend,
  267. .resume = com20020_resume,
  268. };
  269. static int __init init_com20020_cs(void)
  270. {
  271. return pcmcia_register_driver(&com20020_cs_driver);
  272. }
  273. static void __exit exit_com20020_cs(void)
  274. {
  275. pcmcia_unregister_driver(&com20020_cs_driver);
  276. }
  277. module_init(init_com20020_cs);
  278. module_exit(exit_com20020_cs);