com20020_cs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 PCMCIA_DEBUG
  53. static int pc_debug = PCMCIA_DEBUG;
  54. module_param(pc_debug, int, 0);
  55. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  56. static void regdump(struct net_device *dev)
  57. {
  58. int ioaddr = dev->base_addr;
  59. int count;
  60. printk("com20020 register dump:\n");
  61. for (count = ioaddr; count < ioaddr + 16; count++)
  62. {
  63. if (!(count % 16))
  64. printk("\n%04X: ", count);
  65. printk("%02X ", inb(count));
  66. }
  67. printk("\n");
  68. printk("buffer0 dump:\n");
  69. /* set up the address register */
  70. count = 0;
  71. outb((count >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
  72. outb(count & 0xff, _ADDR_LO);
  73. for (count = 0; count < 256+32; count++)
  74. {
  75. if (!(count % 16))
  76. printk("\n%04X: ", count);
  77. /* copy the data */
  78. printk("%02X ", inb(_MEMDATA));
  79. }
  80. printk("\n");
  81. }
  82. #else
  83. #define DEBUG(n, args...) do { } while (0)
  84. static inline void regdump(struct net_device *dev) { }
  85. #endif
  86. /*====================================================================*/
  87. /* Parameters that can be set with 'insmod' */
  88. static int node;
  89. static int timeout = 3;
  90. static int backplane;
  91. static int clockp;
  92. static int clockm;
  93. module_param(node, int, 0);
  94. module_param(timeout, int, 0);
  95. module_param(backplane, int, 0);
  96. module_param(clockp, int, 0);
  97. module_param(clockm, int, 0);
  98. MODULE_LICENSE("GPL");
  99. /*====================================================================*/
  100. static void com20020_config(dev_link_t *link);
  101. static void com20020_release(dev_link_t *link);
  102. static int com20020_event(event_t event, int priority,
  103. event_callback_args_t *args);
  104. static dev_info_t dev_info = "com20020_cs";
  105. static dev_link_t *com20020_attach(void);
  106. static void com20020_detach(struct pcmcia_device *p_dev);
  107. static dev_link_t *dev_list;
  108. /*====================================================================*/
  109. typedef struct com20020_dev_t {
  110. struct net_device *dev;
  111. dev_node_t node;
  112. } com20020_dev_t;
  113. /*======================================================================
  114. com20020_attach() creates an "instance" of the driver, allocating
  115. local data structures for one device. The device is registered
  116. with Card Services.
  117. ======================================================================*/
  118. static dev_link_t *com20020_attach(void)
  119. {
  120. client_reg_t client_reg;
  121. dev_link_t *link;
  122. com20020_dev_t *info;
  123. struct net_device *dev;
  124. int ret;
  125. struct arcnet_local *lp;
  126. DEBUG(0, "com20020_attach()\n");
  127. /* Create new network device */
  128. link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
  129. if (!link)
  130. return NULL;
  131. info = kmalloc(sizeof(struct com20020_dev_t), GFP_KERNEL);
  132. if (!info)
  133. goto fail_alloc_info;
  134. dev = alloc_arcdev("");
  135. if (!dev)
  136. goto fail_alloc_dev;
  137. memset(info, 0, sizeof(struct com20020_dev_t));
  138. memset(link, 0, sizeof(struct dev_link_t));
  139. lp = dev->priv;
  140. lp->timeout = timeout;
  141. lp->backplane = backplane;
  142. lp->clockp = clockp;
  143. lp->clockm = clockm & 3;
  144. lp->hw.owner = THIS_MODULE;
  145. /* fill in our module parameters as defaults */
  146. dev->dev_addr[0] = node;
  147. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  148. link->io.NumPorts1 = 16;
  149. link->io.IOAddrLines = 16;
  150. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  151. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  152. link->conf.Attributes = CONF_ENABLE_IRQ;
  153. link->conf.Vcc = 50;
  154. link->conf.IntType = INT_MEMORY_AND_IO;
  155. link->conf.Present = PRESENT_OPTION;
  156. link->irq.Instance = info->dev = dev;
  157. link->priv = info;
  158. /* Register with Card Services */
  159. link->next = dev_list;
  160. dev_list = link;
  161. client_reg.dev_info = &dev_info;
  162. client_reg.Version = 0x0210;
  163. client_reg.event_callback_args.client_data = link;
  164. ret = pcmcia_register_client(&link->handle, &client_reg);
  165. if (ret != 0) {
  166. cs_error(link->handle, RegisterClient, ret);
  167. com20020_detach(link->handle);
  168. return NULL;
  169. }
  170. return link;
  171. fail_alloc_dev:
  172. kfree(info);
  173. fail_alloc_info:
  174. kfree(link);
  175. return NULL;
  176. } /* com20020_attach */
  177. /*======================================================================
  178. This deletes a driver "instance". The device is de-registered
  179. with Card Services. If it has been released, all local data
  180. structures are freed. Otherwise, the structures will be freed
  181. when the device is released.
  182. ======================================================================*/
  183. static void com20020_detach(struct pcmcia_device *p_dev)
  184. {
  185. dev_link_t *link = dev_to_instance(p_dev);
  186. struct com20020_dev_t *info = link->priv;
  187. dev_link_t **linkp;
  188. struct net_device *dev;
  189. DEBUG(1,"detach...\n");
  190. DEBUG(0, "com20020_detach(0x%p)\n", link);
  191. /* Locate device structure */
  192. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  193. if (*linkp == link) break;
  194. if (*linkp == NULL)
  195. return;
  196. dev = info->dev;
  197. if (link->dev) {
  198. DEBUG(1,"unregister...\n");
  199. unregister_netdev(dev);
  200. /*
  201. * this is necessary because we register our IRQ separately
  202. * from card services.
  203. */
  204. if (dev->irq)
  205. free_irq(dev->irq, dev);
  206. }
  207. if (link->state & DEV_CONFIG)
  208. com20020_release(link);
  209. /* Unlink device structure, free bits */
  210. DEBUG(1,"unlinking...\n");
  211. *linkp = link->next;
  212. if (link->priv)
  213. {
  214. dev = info->dev;
  215. if (dev)
  216. {
  217. DEBUG(1,"kfree...\n");
  218. free_netdev(dev);
  219. }
  220. DEBUG(1,"kfree2...\n");
  221. kfree(info);
  222. }
  223. DEBUG(1,"kfree3...\n");
  224. kfree(link);
  225. } /* com20020_detach */
  226. /*======================================================================
  227. com20020_config() is scheduled to run after a CARD_INSERTION event
  228. is received, to configure the PCMCIA socket, and to make the
  229. device available to the system.
  230. ======================================================================*/
  231. #define CS_CHECK(fn, ret) \
  232. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  233. static void com20020_config(dev_link_t *link)
  234. {
  235. struct arcnet_local *lp;
  236. client_handle_t handle;
  237. tuple_t tuple;
  238. cisparse_t parse;
  239. com20020_dev_t *info;
  240. struct net_device *dev;
  241. int i, last_ret, last_fn;
  242. u_char buf[64];
  243. int ioaddr;
  244. handle = link->handle;
  245. info = link->priv;
  246. dev = info->dev;
  247. DEBUG(1,"config...\n");
  248. DEBUG(0, "com20020_config(0x%p)\n", link);
  249. tuple.Attributes = 0;
  250. tuple.TupleData = buf;
  251. tuple.TupleDataMax = 64;
  252. tuple.TupleOffset = 0;
  253. tuple.DesiredTuple = CISTPL_CONFIG;
  254. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  255. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  256. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  257. link->conf.ConfigBase = parse.config.base;
  258. /* Configure card */
  259. link->state |= DEV_CONFIG;
  260. DEBUG(1,"arcnet: baseport1 is %Xh\n", link->io.BasePort1);
  261. i = !CS_SUCCESS;
  262. if (!link->io.BasePort1)
  263. {
  264. for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10)
  265. {
  266. link->io.BasePort1 = ioaddr;
  267. i = pcmcia_request_io(link->handle, &link->io);
  268. if (i == CS_SUCCESS)
  269. break;
  270. }
  271. }
  272. else
  273. i = pcmcia_request_io(link->handle, &link->io);
  274. if (i != CS_SUCCESS)
  275. {
  276. DEBUG(1,"arcnet: requestIO failed totally!\n");
  277. goto failed;
  278. }
  279. ioaddr = dev->base_addr = link->io.BasePort1;
  280. DEBUG(1,"arcnet: got ioaddr %Xh\n", ioaddr);
  281. DEBUG(1,"arcnet: request IRQ %d (%Xh/%Xh)\n",
  282. link->irq.AssignedIRQ,
  283. link->irq.IRQInfo1, link->irq.IRQInfo2);
  284. i = pcmcia_request_irq(link->handle, &link->irq);
  285. if (i != CS_SUCCESS)
  286. {
  287. DEBUG(1,"arcnet: requestIRQ failed totally!\n");
  288. goto failed;
  289. }
  290. dev->irq = link->irq.AssignedIRQ;
  291. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  292. if (com20020_check(dev))
  293. {
  294. regdump(dev);
  295. goto failed;
  296. }
  297. lp = dev->priv;
  298. lp->card_name = "PCMCIA COM20020";
  299. lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
  300. link->dev = &info->node;
  301. link->state &= ~DEV_CONFIG_PENDING;
  302. SET_NETDEV_DEV(dev, &handle_to_dev(handle));
  303. i = com20020_found(dev, 0); /* calls register_netdev */
  304. if (i != 0) {
  305. DEBUG(1,KERN_NOTICE "com20020_cs: com20020_found() failed\n");
  306. link->dev = NULL;
  307. goto failed;
  308. }
  309. strcpy(info->node.dev_name, dev->name);
  310. DEBUG(1,KERN_INFO "%s: port %#3lx, irq %d\n",
  311. dev->name, dev->base_addr, dev->irq);
  312. return;
  313. cs_failed:
  314. cs_error(link->handle, last_fn, last_ret);
  315. failed:
  316. DEBUG(1,"com20020_config failed...\n");
  317. com20020_release(link);
  318. } /* com20020_config */
  319. /*======================================================================
  320. After a card is removed, com20020_release() will unregister the net
  321. device, and release the PCMCIA configuration. If the device is
  322. still open, this will be postponed until it is closed.
  323. ======================================================================*/
  324. static void com20020_release(dev_link_t *link)
  325. {
  326. DEBUG(1,"release...\n");
  327. DEBUG(0, "com20020_release(0x%p)\n", link);
  328. pcmcia_release_configuration(link->handle);
  329. pcmcia_release_io(link->handle, &link->io);
  330. pcmcia_release_irq(link->handle, &link->irq);
  331. link->state &= ~(DEV_CONFIG | DEV_RELEASE_PENDING);
  332. }
  333. static int com20020_suspend(struct pcmcia_device *p_dev)
  334. {
  335. dev_link_t *link = dev_to_instance(p_dev);
  336. com20020_dev_t *info = link->priv;
  337. struct net_device *dev = info->dev;
  338. link->state |= DEV_SUSPEND;
  339. if (link->state & DEV_CONFIG) {
  340. if (link->open) {
  341. netif_device_detach(dev);
  342. }
  343. pcmcia_release_configuration(link->handle);
  344. }
  345. return 0;
  346. }
  347. static int com20020_resume(struct pcmcia_device *p_dev)
  348. {
  349. dev_link_t *link = dev_to_instance(p_dev);
  350. com20020_dev_t *info = link->priv;
  351. struct net_device *dev = info->dev;
  352. link->state &= ~DEV_SUSPEND;
  353. if (link->state & DEV_CONFIG) {
  354. pcmcia_request_configuration(link->handle, &link->conf);
  355. if (link->open) {
  356. int ioaddr = dev->base_addr;
  357. struct arcnet_local *lp = dev->priv;
  358. ARCRESET;
  359. }
  360. }
  361. return 0;
  362. }
  363. /*======================================================================
  364. The card status event handler. Mostly, this schedules other
  365. stuff to run after an event is received. A CARD_REMOVAL event
  366. also sets some flags to discourage the net drivers from trying
  367. to talk to the card any more.
  368. ======================================================================*/
  369. static int com20020_event(event_t event, int priority,
  370. event_callback_args_t *args)
  371. {
  372. dev_link_t *link = args->client_data;
  373. DEBUG(1, "com20020_event(0x%06x)\n", event);
  374. switch (event) {
  375. case CS_EVENT_CARD_INSERTION:
  376. link->state |= DEV_PRESENT;
  377. com20020_config(link);
  378. break;
  379. }
  380. return 0;
  381. } /* com20020_event */
  382. static struct pcmcia_device_id com20020_ids[] = {
  383. PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.", "PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
  384. PCMCIA_DEVICE_NULL
  385. };
  386. MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
  387. static struct pcmcia_driver com20020_cs_driver = {
  388. .owner = THIS_MODULE,
  389. .drv = {
  390. .name = "com20020_cs",
  391. },
  392. .attach = com20020_attach,
  393. .event = com20020_event,
  394. .remove = com20020_detach,
  395. .id_table = com20020_ids,
  396. .suspend = com20020_suspend,
  397. .resume = com20020_resume,
  398. };
  399. static int __init init_com20020_cs(void)
  400. {
  401. return pcmcia_register_driver(&com20020_cs_driver);
  402. }
  403. static void __exit exit_com20020_cs(void)
  404. {
  405. pcmcia_unregister_driver(&com20020_cs_driver);
  406. BUG_ON(dev_list != NULL);
  407. }
  408. module_init(init_com20020_cs);
  409. module_exit(exit_com20020_cs);