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