atmel_cs.c 17 KB

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