com20020_cs.c 9.9 KB

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