atmel_cs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*** -*- linux-c -*- **********************************************************
  2. Driver for Atmel at76c502 at76c504 and at76c506 wireless cards.
  3. Copyright 2000-2001 ATMEL Corporation.
  4. Copyright 2003 Simon Kelley.
  5. This code was developed from version 2.1.1 of the Atmel drivers,
  6. released by Atmel corp. under the GPL in December 2002. It also
  7. includes code from the Linux aironet drivers (C) Benjamin Reed,
  8. and the Linux PCMCIA package, (C) David Hinds.
  9. For all queries about this code, please contact the current author,
  10. Simon Kelley <simon@thekelleys.org.uk> and not Atmel Corporation.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This software is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with Atmel wireless lan drivers; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. ******************************************************************************/
  23. #ifdef __IN_PCMCIA_PACKAGE__
  24. #include <pcmcia/k_compat.h>
  25. #endif
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/slab.h>
  31. #include <linux/string.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/device.h>
  35. #include <pcmcia/cs_types.h>
  36. #include <pcmcia/cs.h>
  37. #include <pcmcia/cistpl.h>
  38. #include <pcmcia/cisreg.h>
  39. #include <pcmcia/ds.h>
  40. #include <pcmcia/ciscode.h>
  41. #include <asm/io.h>
  42. #include <asm/system.h>
  43. #include <linux/wireless.h>
  44. #include "atmel.h"
  45. /*
  46. All the PCMCIA modules use PCMCIA_DEBUG to control debugging. If
  47. you do not define PCMCIA_DEBUG at all, all the debug code will be
  48. left out. If you compile with PCMCIA_DEBUG=0, the debug code will
  49. be present but disabled -- but it can then be enabled for specific
  50. modules at load time with a 'pc_debug=#' option to insmod.
  51. */
  52. #ifdef PCMCIA_DEBUG
  53. static int pc_debug = PCMCIA_DEBUG;
  54. module_param(pc_debug, int, 0);
  55. static char *version = "$Revision: 1.2 $";
  56. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
  57. #else
  58. #define DEBUG(n, args...)
  59. #endif
  60. /*====================================================================*/
  61. MODULE_AUTHOR("Simon Kelley");
  62. MODULE_DESCRIPTION("Support for Atmel at76c50x 802.11 wireless ethernet cards.");
  63. MODULE_LICENSE("GPL");
  64. MODULE_SUPPORTED_DEVICE("Atmel at76c50x PCMCIA cards");
  65. /*====================================================================*/
  66. /*
  67. The event() function is this driver's Card Services event handler.
  68. It will be called by Card Services when an appropriate card status
  69. event is received. The config() and release() entry points are
  70. used to configure or release a socket, in response to card
  71. insertion and ejection events. They are invoked from the atmel_cs
  72. event handler.
  73. */
  74. static int atmel_config(struct pcmcia_device *link);
  75. static void atmel_release(struct pcmcia_device *link);
  76. /*
  77. The attach() and detach() entry points are used to create and destroy
  78. "instances" of the driver, where each instance represents everything
  79. needed to manage one actual PCMCIA card.
  80. */
  81. static void atmel_detach(struct pcmcia_device *p_dev);
  82. /*
  83. You'll also need to prototype all the functions that will actually
  84. be used to talk to your device. See 'pcmem_cs' for a good example
  85. of a fully self-sufficient driver; the other drivers rely more or
  86. less on other parts of the kernel.
  87. */
  88. /*
  89. A linked list of "instances" of the atmelnet device. Each actual
  90. PCMCIA card corresponds to one device instance, and is described
  91. by one struct pcmcia_device structure (defined in ds.h).
  92. You may not want to use a linked list for this -- for example, the
  93. memory card driver uses an array of struct pcmcia_device pointers, where minor
  94. device numbers are used to derive the corresponding array index.
  95. */
  96. /*
  97. A driver needs to provide a dev_node_t structure for each device
  98. on a card. In some cases, there is only one device per card (for
  99. example, ethernet cards, modems). In other cases, there may be
  100. many actual or logical devices (SCSI adapters, memory cards with
  101. multiple partitions). The dev_node_t structures need to be kept
  102. in a linked list starting at the 'dev' field of a struct pcmcia_device
  103. structure. We allocate them in the card's private data structure,
  104. because they generally shouldn't be allocated dynamically.
  105. In this case, we also provide a flag to indicate if a device is
  106. "stopped" due to a power management event, or card ejection. The
  107. device IO routines can use a flag like this to throttle IO to a
  108. card that is not ready to accept it.
  109. */
  110. typedef struct local_info_t {
  111. dev_node_t node;
  112. struct net_device *eth_dev;
  113. } local_info_t;
  114. /*======================================================================
  115. atmel_attach() creates an "instance" of the driver, allocating
  116. local data structures for one device. The device is registered
  117. with Card Services.
  118. The dev_link structure is initialized, but we don't actually
  119. configure the card at this point -- we wait until we receive a
  120. card insertion event.
  121. ======================================================================*/
  122. static int atmel_probe(struct pcmcia_device *p_dev)
  123. {
  124. local_info_t *local;
  125. DEBUG(0, "atmel_attach()\n");
  126. /* Interrupt setup */
  127. p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
  128. p_dev->irq.IRQInfo1 = IRQ_LEVEL_ID;
  129. p_dev->irq.Handler = NULL;
  130. /*
  131. General socket configuration defaults can go here. In this
  132. client, we assume very little, and rely on the CIS for almost
  133. everything. In most clients, many details (i.e., number, sizes,
  134. and attributes of IO windows) are fixed by the nature of the
  135. device, and can be hard-wired here.
  136. */
  137. p_dev->conf.Attributes = 0;
  138. p_dev->conf.IntType = INT_MEMORY_AND_IO;
  139. /* Allocate space for private device-specific data */
  140. local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
  141. if (!local) {
  142. printk(KERN_ERR "atmel_cs: no memory for new device\n");
  143. return -ENOMEM;
  144. }
  145. p_dev->priv = local;
  146. return atmel_config(p_dev);
  147. } /* atmel_attach */
  148. /*======================================================================
  149. This deletes a driver "instance". The device is de-registered
  150. with Card Services. If it has been released, all local data
  151. structures are freed. Otherwise, the structures will be freed
  152. when the device is released.
  153. ======================================================================*/
  154. static void atmel_detach(struct pcmcia_device *link)
  155. {
  156. DEBUG(0, "atmel_detach(0x%p)\n", link);
  157. atmel_release(link);
  158. kfree(link->priv);
  159. }
  160. /*======================================================================
  161. atmel_config() is scheduled to run after a CARD_INSERTION event
  162. is received, to configure the PCMCIA socket, and to make the
  163. device available to the system.
  164. ======================================================================*/
  165. #define CS_CHECK(fn, ret) \
  166. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  167. /* Call-back function to interrogate PCMCIA-specific information
  168. about the current existance of the card */
  169. static int card_present(void *arg)
  170. {
  171. struct pcmcia_device *link = (struct pcmcia_device *)arg;
  172. if (pcmcia_dev_present(link))
  173. return 1;
  174. return 0;
  175. }
  176. static int atmel_config_check(struct pcmcia_device *p_dev,
  177. cistpl_cftable_entry_t *cfg,
  178. cistpl_cftable_entry_t *dflt,
  179. unsigned int vcc,
  180. void *priv_data)
  181. {
  182. if (cfg->index == 0)
  183. return -ENODEV;
  184. /* Does this card need audio output? */
  185. if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
  186. p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
  187. p_dev->conf.Status = CCSR_AUDIO_ENA;
  188. }
  189. /* Use power settings for Vcc and Vpp if present */
  190. /* Note that the CIS values need to be rescaled */
  191. if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
  192. p_dev->conf.Vpp = cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
  193. else if (dflt->vpp1.present & (1<<CISTPL_POWER_VNOM))
  194. p_dev->conf.Vpp = dflt->vpp1.param[CISTPL_POWER_VNOM]/10000;
  195. /* Do we need to allocate an interrupt? */
  196. if (cfg->irq.IRQInfo1 || dflt->irq.IRQInfo1)
  197. p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
  198. /* IO window settings */
  199. p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
  200. if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
  201. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
  202. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  203. if (!(io->flags & CISTPL_IO_8BIT))
  204. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
  205. if (!(io->flags & CISTPL_IO_16BIT))
  206. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  207. p_dev->io.BasePort1 = io->win[0].base;
  208. p_dev->io.NumPorts1 = io->win[0].len;
  209. if (io->nwin > 1) {
  210. p_dev->io.Attributes2 = p_dev->io.Attributes1;
  211. p_dev->io.BasePort2 = io->win[1].base;
  212. p_dev->io.NumPorts2 = io->win[1].len;
  213. }
  214. }
  215. /* This reserves IO space but doesn't actually enable it */
  216. return pcmcia_request_io(p_dev, &p_dev->io);
  217. }
  218. static int atmel_config(struct pcmcia_device *link)
  219. {
  220. local_info_t *dev;
  221. int last_fn, last_ret;
  222. struct pcmcia_device_id *did;
  223. dev = link->priv;
  224. did = dev_get_drvdata(&handle_to_dev(link));
  225. DEBUG(0, "atmel_config(0x%p)\n", link);
  226. /*
  227. In this loop, we scan the CIS for configuration table entries,
  228. each of which describes a valid card configuration, including
  229. voltage, IO window, memory window, and interrupt settings.
  230. We make no assumptions about the card to be configured: we use
  231. just the information available in the CIS. In an ideal world,
  232. this would work for any PCMCIA card, but it requires a complete
  233. and accurate CIS. In practice, a driver usually "knows" most of
  234. these things without consulting the CIS, and most client drivers
  235. will only use the CIS to fill in implementation-defined details.
  236. */
  237. if (pcmcia_loop_config(link, atmel_config_check, NULL))
  238. goto failed;
  239. /*
  240. Allocate an interrupt line. Note that this does not assign a
  241. handler to the interrupt, unless the 'Handler' member of the
  242. irq structure is initialized.
  243. */
  244. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  245. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  246. /*
  247. This actually configures the PCMCIA socket -- setting up
  248. the I/O windows and the interrupt mapping, and putting the
  249. card and host interface into "Memory and IO" mode.
  250. */
  251. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  252. if (link->irq.AssignedIRQ == 0) {
  253. printk(KERN_ALERT
  254. "atmel: cannot assign IRQ: check that CONFIG_ISA is set in kernel config.");
  255. goto cs_failed;
  256. }
  257. ((local_info_t*)link->priv)->eth_dev =
  258. init_atmel_card(link->irq.AssignedIRQ,
  259. link->io.BasePort1,
  260. did ? did->driver_info : ATMEL_FW_TYPE_NONE,
  261. &handle_to_dev(link),
  262. card_present,
  263. link);
  264. if (!((local_info_t*)link->priv)->eth_dev)
  265. goto cs_failed;
  266. /*
  267. At this point, the dev_node_t structure(s) need to be
  268. initialized and arranged in a linked list at link->dev_node.
  269. */
  270. strcpy(dev->node.dev_name, ((local_info_t*)link->priv)->eth_dev->name );
  271. dev->node.major = dev->node.minor = 0;
  272. link->dev_node = &dev->node;
  273. return 0;
  274. cs_failed:
  275. cs_error(link, last_fn, last_ret);
  276. failed:
  277. atmel_release(link);
  278. return -ENODEV;
  279. }
  280. /*======================================================================
  281. After a card is removed, atmel_release() will unregister the
  282. device, and release the PCMCIA configuration. If the device is
  283. still open, this will be postponed until it is closed.
  284. ======================================================================*/
  285. static void atmel_release(struct pcmcia_device *link)
  286. {
  287. struct net_device *dev = ((local_info_t*)link->priv)->eth_dev;
  288. DEBUG(0, "atmel_release(0x%p)\n", link);
  289. if (dev)
  290. stop_atmel_card(dev);
  291. ((local_info_t*)link->priv)->eth_dev = NULL;
  292. pcmcia_disable_device(link);
  293. }
  294. static int atmel_suspend(struct pcmcia_device *link)
  295. {
  296. local_info_t *local = link->priv;
  297. netif_device_detach(local->eth_dev);
  298. return 0;
  299. }
  300. static int atmel_resume(struct pcmcia_device *link)
  301. {
  302. local_info_t *local = link->priv;
  303. atmel_open(local->eth_dev);
  304. netif_device_attach(local->eth_dev);
  305. return 0;
  306. }
  307. /*====================================================================*/
  308. /* We use the driver_info field to store the correct firmware type for a card. */
  309. #define PCMCIA_DEVICE_MANF_CARD_INFO(manf, card, info) { \
  310. .match_flags = PCMCIA_DEV_ID_MATCH_MANF_ID| \
  311. PCMCIA_DEV_ID_MATCH_CARD_ID, \
  312. .manf_id = (manf), \
  313. .card_id = (card), \
  314. .driver_info = (kernel_ulong_t)(info), }
  315. #define PCMCIA_DEVICE_PROD_ID12_INFO(v1, v2, vh1, vh2, info) { \
  316. .match_flags = PCMCIA_DEV_ID_MATCH_PROD_ID1| \
  317. PCMCIA_DEV_ID_MATCH_PROD_ID2, \
  318. .prod_id = { (v1), (v2), NULL, NULL }, \
  319. .prod_id_hash = { (vh1), (vh2), 0, 0 }, \
  320. .driver_info = (kernel_ulong_t)(info), }
  321. static struct pcmcia_device_id atmel_ids[] = {
  322. PCMCIA_DEVICE_MANF_CARD_INFO(0x0101, 0x0620, ATMEL_FW_TYPE_502_3COM),
  323. PCMCIA_DEVICE_MANF_CARD_INFO(0x0101, 0x0696, ATMEL_FW_TYPE_502_3COM),
  324. PCMCIA_DEVICE_MANF_CARD_INFO(0x01bf, 0x3302, ATMEL_FW_TYPE_502E),
  325. PCMCIA_DEVICE_MANF_CARD_INFO(0xd601, 0x0007, ATMEL_FW_TYPE_502),
  326. PCMCIA_DEVICE_PROD_ID12_INFO("11WAVE", "11WP611AL-E", 0x9eb2da1f, 0xc9a0d3f9, ATMEL_FW_TYPE_502E),
  327. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C502AR", 0xabda4164, 0x41b37e1f, ATMEL_FW_TYPE_502),
  328. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C502AR_D", 0xabda4164, 0x3675d704, ATMEL_FW_TYPE_502D),
  329. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C502AR_E", 0xabda4164, 0x4172e792, ATMEL_FW_TYPE_502E),
  330. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C504_R", 0xabda4164, 0x917f3d72, ATMEL_FW_TYPE_504_2958),
  331. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C504", 0xabda4164, 0x5040670a, ATMEL_FW_TYPE_504),
  332. PCMCIA_DEVICE_PROD_ID12_INFO("ATMEL", "AT76C504A", 0xabda4164, 0xe15ed87f, ATMEL_FW_TYPE_504A_2958),
  333. PCMCIA_DEVICE_PROD_ID12_INFO("BT", "Voyager 1020 Laptop Adapter", 0xae49b86a, 0x1e957cd5, ATMEL_FW_TYPE_502),
  334. PCMCIA_DEVICE_PROD_ID12_INFO("CNet", "CNWLC 11Mbps Wireless PC Card V-5", 0xbc477dde, 0x502fae6b, ATMEL_FW_TYPE_502E),
  335. PCMCIA_DEVICE_PROD_ID12_INFO("IEEE 802.11b", "Wireless LAN PC Card", 0x5b878724, 0x122f1df6, ATMEL_FW_TYPE_502),
  336. PCMCIA_DEVICE_PROD_ID12_INFO("IEEE 802.11b", "Wireless LAN Card S", 0x5b878724, 0x5fba533a, ATMEL_FW_TYPE_504_2958),
  337. PCMCIA_DEVICE_PROD_ID12_INFO("OEM", "11Mbps Wireless LAN PC Card V-3", 0xfea54c90, 0x1c5b0f68, ATMEL_FW_TYPE_502),
  338. PCMCIA_DEVICE_PROD_ID12_INFO("SMC", "2632W", 0xc4f8b18b, 0x30f38774, ATMEL_FW_TYPE_502D),
  339. PCMCIA_DEVICE_PROD_ID12_INFO("SMC", "2632W-V2", 0xc4f8b18b, 0x172d1377, ATMEL_FW_TYPE_502),
  340. PCMCIA_DEVICE_PROD_ID12_INFO("Wireless", "PC_CARD", 0xa407ecdd, 0x119f6314, ATMEL_FW_TYPE_502D),
  341. PCMCIA_DEVICE_PROD_ID12_INFO("WLAN", "802.11b PC CARD", 0x575c516c, 0xb1f6dbc4, ATMEL_FW_TYPE_502D),
  342. PCMCIA_DEVICE_PROD_ID12_INFO("LG", "LW2100N", 0xb474d43a, 0x6b1fec94, ATMEL_FW_TYPE_502E),
  343. PCMCIA_DEVICE_NULL
  344. };
  345. MODULE_DEVICE_TABLE(pcmcia, atmel_ids);
  346. static struct pcmcia_driver atmel_driver = {
  347. .owner = THIS_MODULE,
  348. .drv = {
  349. .name = "atmel_cs",
  350. },
  351. .probe = atmel_probe,
  352. .remove = atmel_detach,
  353. .id_table = atmel_ids,
  354. .suspend = atmel_suspend,
  355. .resume = atmel_resume,
  356. };
  357. static int atmel_cs_init(void)
  358. {
  359. return pcmcia_register_driver(&atmel_driver);
  360. }
  361. static void atmel_cs_cleanup(void)
  362. {
  363. pcmcia_unregister_driver(&atmel_driver);
  364. }
  365. /*
  366. This program is free software; you can redistribute it and/or
  367. modify it under the terms of the GNU General Public License
  368. as published by the Free Software Foundation; either version 2
  369. of the License, or (at your option) any later version.
  370. This program is distributed in the hope that it will be useful,
  371. but WITHOUT ANY WARRANTY; without even the implied warranty of
  372. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  373. GNU General Public License for more details.
  374. In addition:
  375. Redistribution and use in source and binary forms, with or without
  376. modification, are permitted provided that the following conditions
  377. are met:
  378. 1. Redistributions of source code must retain the above copyright
  379. notice, this list of conditions and the following disclaimer.
  380. 2. Redistributions in binary form must reproduce the above copyright
  381. notice, this list of conditions and the following disclaimer in the
  382. documentation and/or other materials provided with the distribution.
  383. 3. The name of the author may not be used to endorse or promote
  384. products derived from this software without specific prior written
  385. permission.
  386. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  387. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  388. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  389. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  390. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  391. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  392. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  393. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  394. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  395. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  396. POSSIBILITY OF SUCH DAMAGE.
  397. */
  398. module_init(atmel_cs_init);
  399. module_exit(atmel_cs_cleanup);