main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * IPWireless 3G PCMCIA Network Driver
  3. *
  4. * Original code
  5. * by Stephen Blackheath <stephen@blacksapphire.com>,
  6. * Ben Martel <benm@symmetric.co.nz>
  7. *
  8. * Copyrighted as follows:
  9. * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
  10. *
  11. * Various driver changes and rewrites, port to new kernels
  12. * Copyright (C) 2006-2007 Jiri Kosina
  13. *
  14. * Misc code cleanups and updates
  15. * Copyright (C) 2007 David Sterba
  16. */
  17. #include "hardware.h"
  18. #include "network.h"
  19. #include "main.h"
  20. #include "tty.h"
  21. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <pcmcia/cisreg.h>
  29. #include <pcmcia/device_id.h>
  30. #include <pcmcia/ss.h>
  31. #include <pcmcia/ds.h>
  32. #include <pcmcia/cs.h>
  33. static struct pcmcia_device_id ipw_ids[] = {
  34. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
  35. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
  36. PCMCIA_DEVICE_NULL
  37. };
  38. MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
  39. static void ipwireless_detach(struct pcmcia_device *link);
  40. /*
  41. * Module params
  42. */
  43. /* Debug mode: more verbose, print sent/recv bytes */
  44. int ipwireless_debug;
  45. int ipwireless_loopback;
  46. int ipwireless_out_queue = 10;
  47. module_param_named(debug, ipwireless_debug, int, 0);
  48. module_param_named(loopback, ipwireless_loopback, int, 0);
  49. module_param_named(out_queue, ipwireless_out_queue, int, 0);
  50. MODULE_PARM_DESC(debug, "switch on debug messages [0]");
  51. MODULE_PARM_DESC(loopback,
  52. "debug: enable ras_raw channel [0]");
  53. MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
  54. /* Executes in process context. */
  55. static void signalled_reboot_work(struct work_struct *work_reboot)
  56. {
  57. struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
  58. work_reboot);
  59. struct pcmcia_device *link = ipw->link;
  60. pcmcia_reset_card(link->socket);
  61. }
  62. static void signalled_reboot_callback(void *callback_data)
  63. {
  64. struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
  65. /* Delegate to process context. */
  66. schedule_work(&ipw->work_reboot);
  67. }
  68. static int ipwireless_probe(struct pcmcia_device *p_dev,
  69. cistpl_cftable_entry_t *cfg,
  70. cistpl_cftable_entry_t *dflt,
  71. unsigned int vcc,
  72. void *priv_data)
  73. {
  74. struct ipw_dev *ipw = priv_data;
  75. struct resource *io_resource;
  76. memreq_t memreq_attr_memory;
  77. memreq_t memreq_common_memory;
  78. int ret;
  79. p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  80. p_dev->io.BasePort1 = cfg->io.win[0].base;
  81. p_dev->io.NumPorts1 = cfg->io.win[0].len;
  82. p_dev->io.IOAddrLines = 16;
  83. /* 0x40 causes it to generate level mode interrupts. */
  84. /* 0x04 enables IREQ pin. */
  85. p_dev->conf.ConfigIndex = cfg->index | 0x44;
  86. ret = pcmcia_request_io(p_dev, &p_dev->io);
  87. if (ret)
  88. return ret;
  89. io_resource = request_region(p_dev->io.BasePort1, p_dev->io.NumPorts1,
  90. IPWIRELESS_PCCARD_NAME);
  91. if (cfg->mem.nwin == 0)
  92. return 0;
  93. ipw->request_common_memory.Attributes =
  94. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  95. ipw->request_common_memory.Base = cfg->mem.win[0].host_addr;
  96. ipw->request_common_memory.Size = cfg->mem.win[0].len;
  97. if (ipw->request_common_memory.Size < 0x1000)
  98. ipw->request_common_memory.Size = 0x1000;
  99. ipw->request_common_memory.AccessSpeed = 0;
  100. ret = pcmcia_request_window(p_dev, &ipw->request_common_memory,
  101. &ipw->handle_common_memory);
  102. if (ret != 0)
  103. goto exit1;
  104. memreq_common_memory.CardOffset = cfg->mem.win[0].card_addr;
  105. memreq_common_memory.Page = 0;
  106. ret = pcmcia_map_mem_page(p_dev, ipw->handle_common_memory,
  107. &memreq_common_memory);
  108. if (ret != 0)
  109. goto exit2;
  110. ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
  111. ipw->common_memory = ioremap(ipw->request_common_memory.Base,
  112. ipw->request_common_memory.Size);
  113. request_mem_region(ipw->request_common_memory.Base,
  114. ipw->request_common_memory.Size,
  115. IPWIRELESS_PCCARD_NAME);
  116. ipw->request_attr_memory.Attributes =
  117. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
  118. ipw->request_attr_memory.Base = 0;
  119. ipw->request_attr_memory.Size = 0; /* this used to be 0x1000 */
  120. ipw->request_attr_memory.AccessSpeed = 0;
  121. ret = pcmcia_request_window(p_dev, &ipw->request_attr_memory,
  122. &ipw->handle_attr_memory);
  123. if (ret != 0)
  124. goto exit2;
  125. memreq_attr_memory.CardOffset = 0;
  126. memreq_attr_memory.Page = 0;
  127. ret = pcmcia_map_mem_page(p_dev, ipw->handle_attr_memory,
  128. &memreq_attr_memory);
  129. if (ret != 0)
  130. goto exit3;
  131. ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
  132. ipw->request_attr_memory.Size);
  133. request_mem_region(ipw->request_attr_memory.Base,
  134. ipw->request_attr_memory.Size, IPWIRELESS_PCCARD_NAME);
  135. return 0;
  136. exit3:
  137. pcmcia_release_window(p_dev, ipw->handle_attr_memory);
  138. exit2:
  139. if (ipw->common_memory) {
  140. release_mem_region(ipw->request_common_memory.Base,
  141. ipw->request_common_memory.Size);
  142. iounmap(ipw->common_memory);
  143. pcmcia_release_window(p_dev, ipw->handle_common_memory);
  144. } else
  145. pcmcia_release_window(p_dev, ipw->handle_common_memory);
  146. exit1:
  147. release_resource(io_resource);
  148. pcmcia_disable_device(p_dev);
  149. return -1;
  150. }
  151. static int config_ipwireless(struct ipw_dev *ipw)
  152. {
  153. struct pcmcia_device *link = ipw->link;
  154. int ret = 0;
  155. ipw->is_v2_card = 0;
  156. ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
  157. if (ret != 0)
  158. return ret;
  159. link->conf.Attributes = CONF_ENABLE_IRQ;
  160. link->conf.IntType = INT_MEMORY_AND_IO;
  161. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  162. ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
  163. ipw->attr_memory, ipw->common_memory,
  164. ipw->is_v2_card, signalled_reboot_callback,
  165. ipw);
  166. ret = pcmcia_request_irq(link, ipwireless_interrupt);
  167. if (ret != 0)
  168. goto exit;
  169. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  170. ipw->is_v2_card ? "V2/V3" : "V1");
  171. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  172. ": I/O ports 0x%04x-0x%04x, irq %d\n",
  173. (unsigned int) link->io.BasePort1,
  174. (unsigned int) (link->io.BasePort1 +
  175. link->io.NumPorts1 - 1),
  176. (unsigned int) link->irq);
  177. if (ipw->attr_memory && ipw->common_memory)
  178. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  179. ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
  180. ipw->request_attr_memory.Base,
  181. ipw->request_attr_memory.Base
  182. + ipw->request_attr_memory.Size - 1,
  183. ipw->request_common_memory.Base,
  184. ipw->request_common_memory.Base
  185. + ipw->request_common_memory.Size - 1);
  186. ipw->network = ipwireless_network_create(ipw->hardware);
  187. if (!ipw->network)
  188. goto exit;
  189. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
  190. if (!ipw->tty)
  191. goto exit;
  192. ipwireless_init_hardware_v2_v3(ipw->hardware);
  193. /*
  194. * Do the RequestConfiguration last, because it enables interrupts.
  195. * Then we don't get any interrupts before we're ready for them.
  196. */
  197. ret = pcmcia_request_configuration(link, &link->conf);
  198. if (ret != 0)
  199. goto exit;
  200. return 0;
  201. exit:
  202. if (ipw->attr_memory) {
  203. release_mem_region(ipw->request_attr_memory.Base,
  204. ipw->request_attr_memory.Size);
  205. iounmap(ipw->attr_memory);
  206. pcmcia_release_window(link, ipw->handle_attr_memory);
  207. }
  208. if (ipw->common_memory) {
  209. release_mem_region(ipw->request_common_memory.Base,
  210. ipw->request_common_memory.Size);
  211. iounmap(ipw->common_memory);
  212. pcmcia_release_window(link, ipw->handle_common_memory);
  213. }
  214. pcmcia_disable_device(link);
  215. return -1;
  216. }
  217. static void release_ipwireless(struct ipw_dev *ipw)
  218. {
  219. if (ipw->common_memory) {
  220. release_mem_region(ipw->request_common_memory.Base,
  221. ipw->request_common_memory.Size);
  222. iounmap(ipw->common_memory);
  223. }
  224. if (ipw->attr_memory) {
  225. release_mem_region(ipw->request_attr_memory.Base,
  226. ipw->request_attr_memory.Size);
  227. iounmap(ipw->attr_memory);
  228. }
  229. if (ipw->common_memory)
  230. pcmcia_release_window(ipw->link, ipw->handle_common_memory);
  231. if (ipw->attr_memory)
  232. pcmcia_release_window(ipw->link, ipw->handle_attr_memory);
  233. pcmcia_disable_device(ipw->link);
  234. }
  235. /*
  236. * ipwireless_attach() creates an "instance" of the driver, allocating
  237. * local data structures for one device (one interface). The device
  238. * is registered with Card Services.
  239. *
  240. * The pcmcia_device structure is initialized, but we don't actually
  241. * configure the card at this point -- we wait until we receive a
  242. * card insertion event.
  243. */
  244. static int ipwireless_attach(struct pcmcia_device *link)
  245. {
  246. struct ipw_dev *ipw;
  247. int ret;
  248. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  249. if (!ipw)
  250. return -ENOMEM;
  251. ipw->link = link;
  252. link->priv = ipw;
  253. ipw->hardware = ipwireless_hardware_create();
  254. if (!ipw->hardware) {
  255. kfree(ipw);
  256. return -ENOMEM;
  257. }
  258. /* RegisterClient will call config_ipwireless */
  259. ret = config_ipwireless(ipw);
  260. if (ret != 0) {
  261. ipwireless_detach(link);
  262. return ret;
  263. }
  264. return 0;
  265. }
  266. /*
  267. * This deletes a driver "instance". The device is de-registered with
  268. * Card Services. If it has been released, all local data structures
  269. * are freed. Otherwise, the structures will be freed when the device
  270. * is released.
  271. */
  272. static void ipwireless_detach(struct pcmcia_device *link)
  273. {
  274. struct ipw_dev *ipw = link->priv;
  275. release_ipwireless(ipw);
  276. if (ipw->tty != NULL)
  277. ipwireless_tty_free(ipw->tty);
  278. if (ipw->network != NULL)
  279. ipwireless_network_free(ipw->network);
  280. if (ipw->hardware != NULL)
  281. ipwireless_hardware_free(ipw->hardware);
  282. kfree(ipw);
  283. }
  284. static struct pcmcia_driver me = {
  285. .owner = THIS_MODULE,
  286. .probe = ipwireless_attach,
  287. .remove = ipwireless_detach,
  288. .drv = { .name = IPWIRELESS_PCCARD_NAME },
  289. .id_table = ipw_ids
  290. };
  291. /*
  292. * Module insertion : initialisation of the module.
  293. * Register the card with cardmgr...
  294. */
  295. static int __init init_ipwireless(void)
  296. {
  297. int ret;
  298. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  299. IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
  300. ret = ipwireless_tty_init();
  301. if (ret != 0)
  302. return ret;
  303. ret = pcmcia_register_driver(&me);
  304. if (ret != 0)
  305. ipwireless_tty_release();
  306. return ret;
  307. }
  308. /*
  309. * Module removal
  310. */
  311. static void __exit exit_ipwireless(void)
  312. {
  313. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  314. IPWIRELESS_PCMCIA_VERSION " removed\n");
  315. pcmcia_unregister_driver(&me);
  316. ipwireless_tty_release();
  317. }
  318. module_init(init_ipwireless);
  319. module_exit(exit_ipwireless);
  320. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  321. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  322. MODULE_LICENSE("GPL");