com20020_cs.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/cs.h>
  46. #include <pcmcia/cistpl.h>
  47. #include <pcmcia/ds.h>
  48. #include <asm/io.h>
  49. #include <asm/system.h>
  50. #define VERSION "arcnet: COM20020 PCMCIA support loaded.\n"
  51. #ifdef DEBUG
  52. static void regdump(struct net_device *dev)
  53. {
  54. int ioaddr = dev->base_addr;
  55. int count;
  56. printk("com20020 register dump:\n");
  57. for (count = ioaddr; count < ioaddr + 16; count++)
  58. {
  59. if (!(count % 16))
  60. printk("\n%04X: ", count);
  61. printk("%02X ", inb(count));
  62. }
  63. printk("\n");
  64. printk("buffer0 dump:\n");
  65. /* set up the address register */
  66. count = 0;
  67. outb((count >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
  68. outb(count & 0xff, _ADDR_LO);
  69. for (count = 0; count < 256+32; count++)
  70. {
  71. if (!(count % 16))
  72. printk("\n%04X: ", count);
  73. /* copy the data */
  74. printk("%02X ", inb(_MEMDATA));
  75. }
  76. printk("\n");
  77. }
  78. #else
  79. static inline void regdump(struct net_device *dev) { }
  80. #endif
  81. /*====================================================================*/
  82. /* Parameters that can be set with 'insmod' */
  83. static int node;
  84. static int timeout = 3;
  85. static int backplane;
  86. static int clockp;
  87. static int clockm;
  88. module_param(node, int, 0);
  89. module_param(timeout, int, 0);
  90. module_param(backplane, int, 0);
  91. module_param(clockp, int, 0);
  92. module_param(clockm, int, 0);
  93. MODULE_LICENSE("GPL");
  94. /*====================================================================*/
  95. static int com20020_config(struct pcmcia_device *link);
  96. static void com20020_release(struct pcmcia_device *link);
  97. static void com20020_detach(struct pcmcia_device *p_dev);
  98. /*====================================================================*/
  99. typedef struct com20020_dev_t {
  100. struct net_device *dev;
  101. } com20020_dev_t;
  102. /*======================================================================
  103. com20020_attach() creates an "instance" of the driver, allocating
  104. local data structures for one device. The device is registered
  105. with Card Services.
  106. ======================================================================*/
  107. static int com20020_probe(struct pcmcia_device *p_dev)
  108. {
  109. com20020_dev_t *info;
  110. struct net_device *dev;
  111. struct arcnet_local *lp;
  112. dev_dbg(&p_dev->dev, "com20020_attach()\n");
  113. /* Create new network device */
  114. info = kzalloc(sizeof(struct com20020_dev_t), GFP_KERNEL);
  115. if (!info)
  116. goto fail_alloc_info;
  117. dev = alloc_arcdev("");
  118. if (!dev)
  119. goto fail_alloc_dev;
  120. lp = netdev_priv(dev);
  121. lp->timeout = timeout;
  122. lp->backplane = backplane;
  123. lp->clockp = clockp;
  124. lp->clockm = clockm & 3;
  125. lp->hw.owner = THIS_MODULE;
  126. /* fill in our module parameters as defaults */
  127. dev->dev_addr[0] = node;
  128. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
  129. p_dev->resource[0]->end = 16;
  130. p_dev->conf.Attributes = CONF_ENABLE_IRQ;
  131. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  132. info->dev = dev;
  133. p_dev->priv = info;
  134. return com20020_config(p_dev);
  135. fail_alloc_dev:
  136. kfree(info);
  137. fail_alloc_info:
  138. return -ENOMEM;
  139. } /* com20020_attach */
  140. /*======================================================================
  141. This deletes a driver "instance". The device is de-registered
  142. with Card Services. If it has been released, all local data
  143. structures are freed. Otherwise, the structures will be freed
  144. when the device is released.
  145. ======================================================================*/
  146. static void com20020_detach(struct pcmcia_device *link)
  147. {
  148. struct com20020_dev_t *info = link->priv;
  149. struct net_device *dev = info->dev;
  150. dev_dbg(&link->dev, "detach...\n");
  151. dev_dbg(&link->dev, "com20020_detach\n");
  152. dev_dbg(&link->dev, "unregister...\n");
  153. unregister_netdev(dev);
  154. /*
  155. * this is necessary because we register our IRQ separately
  156. * from card services.
  157. */
  158. if (dev->irq)
  159. free_irq(dev->irq, dev);
  160. com20020_release(link);
  161. /* Unlink device structure, free bits */
  162. dev_dbg(&link->dev, "unlinking...\n");
  163. if (link->priv)
  164. {
  165. dev = info->dev;
  166. if (dev)
  167. {
  168. dev_dbg(&link->dev, "kfree...\n");
  169. free_netdev(dev);
  170. }
  171. dev_dbg(&link->dev, "kfree2...\n");
  172. kfree(info);
  173. }
  174. } /* com20020_detach */
  175. /*======================================================================
  176. com20020_config() is scheduled to run after a CARD_INSERTION event
  177. is received, to configure the PCMCIA socket, and to make the
  178. device available to the system.
  179. ======================================================================*/
  180. static int com20020_config(struct pcmcia_device *link)
  181. {
  182. struct arcnet_local *lp;
  183. com20020_dev_t *info;
  184. struct net_device *dev;
  185. int i, ret;
  186. int ioaddr;
  187. info = link->priv;
  188. dev = info->dev;
  189. dev_dbg(&link->dev, "config...\n");
  190. dev_dbg(&link->dev, "com20020_config\n");
  191. dev_dbg(&link->dev, "baseport1 is %Xh\n",
  192. (unsigned int) link->resource[0]->start);
  193. i = -ENODEV;
  194. link->io_lines = 16;
  195. if (!link->resource[0]->start)
  196. {
  197. for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10)
  198. {
  199. link->resource[0]->start = ioaddr;
  200. i = pcmcia_request_io(link);
  201. if (i == 0)
  202. break;
  203. }
  204. }
  205. else
  206. i = pcmcia_request_io(link);
  207. if (i != 0)
  208. {
  209. dev_dbg(&link->dev, "requestIO failed totally!\n");
  210. goto failed;
  211. }
  212. ioaddr = dev->base_addr = link->resource[0]->start;
  213. dev_dbg(&link->dev, "got ioaddr %Xh\n", ioaddr);
  214. dev_dbg(&link->dev, "request IRQ %d\n",
  215. link->irq);
  216. if (!link->irq)
  217. {
  218. dev_dbg(&link->dev, "requestIRQ failed totally!\n");
  219. goto failed;
  220. }
  221. dev->irq = link->irq;
  222. ret = pcmcia_request_configuration(link, &link->conf);
  223. if (ret)
  224. goto failed;
  225. if (com20020_check(dev))
  226. {
  227. regdump(dev);
  228. goto failed;
  229. }
  230. lp = netdev_priv(dev);
  231. lp->card_name = "PCMCIA COM20020";
  232. lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
  233. SET_NETDEV_DEV(dev, &link->dev);
  234. i = com20020_found(dev, 0); /* calls register_netdev */
  235. if (i != 0) {
  236. dev_printk(KERN_NOTICE, &link->dev,
  237. "com20020_cs: com20020_found() failed\n");
  238. goto failed;
  239. }
  240. dev_dbg(&link->dev,KERN_INFO "%s: port %#3lx, irq %d\n",
  241. dev->name, dev->base_addr, dev->irq);
  242. return 0;
  243. failed:
  244. dev_dbg(&link->dev, "com20020_config failed...\n");
  245. com20020_release(link);
  246. return -ENODEV;
  247. } /* com20020_config */
  248. /*======================================================================
  249. After a card is removed, com20020_release() will unregister the net
  250. device, and release the PCMCIA configuration. If the device is
  251. still open, this will be postponed until it is closed.
  252. ======================================================================*/
  253. static void com20020_release(struct pcmcia_device *link)
  254. {
  255. dev_dbg(&link->dev, "com20020_release\n");
  256. pcmcia_disable_device(link);
  257. }
  258. static int com20020_suspend(struct pcmcia_device *link)
  259. {
  260. com20020_dev_t *info = link->priv;
  261. struct net_device *dev = info->dev;
  262. if (link->open)
  263. netif_device_detach(dev);
  264. return 0;
  265. }
  266. static int com20020_resume(struct pcmcia_device *link)
  267. {
  268. com20020_dev_t *info = link->priv;
  269. struct net_device *dev = info->dev;
  270. if (link->open) {
  271. int ioaddr = dev->base_addr;
  272. struct arcnet_local *lp = netdev_priv(dev);
  273. ARCRESET;
  274. }
  275. return 0;
  276. }
  277. static struct pcmcia_device_id com20020_ids[] = {
  278. PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.",
  279. "PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
  280. PCMCIA_DEVICE_PROD_ID12("SoHard AG",
  281. "SH ARC PCMCIA", 0xf8991729, 0x69dff0c7),
  282. PCMCIA_DEVICE_NULL
  283. };
  284. MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
  285. static struct pcmcia_driver com20020_cs_driver = {
  286. .owner = THIS_MODULE,
  287. .drv = {
  288. .name = "com20020_cs",
  289. },
  290. .probe = com20020_probe,
  291. .remove = com20020_detach,
  292. .id_table = com20020_ids,
  293. .suspend = com20020_suspend,
  294. .resume = com20020_resume,
  295. };
  296. static int __init init_com20020_cs(void)
  297. {
  298. return pcmcia_register_driver(&com20020_cs_driver);
  299. }
  300. static void __exit exit_com20020_cs(void)
  301. {
  302. pcmcia_unregister_driver(&com20020_cs_driver);
  303. }
  304. module_init(init_com20020_cs);
  305. module_exit(exit_com20020_cs);