main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. static const struct pcmcia_device_id ipw_ids[] = {
  33. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
  34. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
  35. PCMCIA_DEVICE_NULL
  36. };
  37. MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
  38. static void ipwireless_detach(struct pcmcia_device *link);
  39. /*
  40. * Module params
  41. */
  42. /* Debug mode: more verbose, print sent/recv bytes */
  43. int ipwireless_debug;
  44. int ipwireless_loopback;
  45. int ipwireless_out_queue = 10;
  46. module_param_named(debug, ipwireless_debug, int, 0);
  47. module_param_named(loopback, ipwireless_loopback, int, 0);
  48. module_param_named(out_queue, ipwireless_out_queue, int, 0);
  49. MODULE_PARM_DESC(debug, "switch on debug messages [0]");
  50. MODULE_PARM_DESC(loopback,
  51. "debug: enable ras_raw channel [0]");
  52. MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
  53. /* Executes in process context. */
  54. static void signalled_reboot_work(struct work_struct *work_reboot)
  55. {
  56. struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
  57. work_reboot);
  58. struct pcmcia_device *link = ipw->link;
  59. pcmcia_reset_card(link->socket);
  60. }
  61. static void signalled_reboot_callback(void *callback_data)
  62. {
  63. struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
  64. /* Delegate to process context. */
  65. schedule_work(&ipw->work_reboot);
  66. }
  67. static int ipwireless_probe(struct pcmcia_device *p_dev, void *priv_data)
  68. {
  69. struct ipw_dev *ipw = priv_data;
  70. int ret;
  71. p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
  72. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  73. /* 0x40 causes it to generate level mode interrupts. */
  74. /* 0x04 enables IREQ pin. */
  75. p_dev->config_index |= 0x44;
  76. p_dev->io_lines = 16;
  77. ret = pcmcia_request_io(p_dev);
  78. if (ret)
  79. return ret;
  80. if (!request_region(p_dev->resource[0]->start,
  81. resource_size(p_dev->resource[0]),
  82. IPWIRELESS_PCCARD_NAME)) {
  83. ret = -EBUSY;
  84. goto exit;
  85. }
  86. p_dev->resource[2]->flags |=
  87. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  88. ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
  89. if (ret != 0)
  90. goto exit1;
  91. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2], p_dev->card_addr);
  92. if (ret != 0)
  93. goto exit1;
  94. ipw->is_v2_card = resource_size(p_dev->resource[2]) == 0x100;
  95. ipw->common_memory = ioremap(p_dev->resource[2]->start,
  96. resource_size(p_dev->resource[2]));
  97. if (!ipw->common_memory) {
  98. ret = -ENOMEM;
  99. goto exit1;
  100. }
  101. if (!request_mem_region(p_dev->resource[2]->start,
  102. resource_size(p_dev->resource[2]),
  103. IPWIRELESS_PCCARD_NAME)) {
  104. ret = -EBUSY;
  105. goto exit2;
  106. }
  107. p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
  108. WIN_ENABLE;
  109. p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
  110. ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
  111. if (ret != 0)
  112. goto exit3;
  113. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
  114. if (ret != 0)
  115. goto exit3;
  116. ipw->attr_memory = ioremap(p_dev->resource[3]->start,
  117. resource_size(p_dev->resource[3]));
  118. if (!ipw->attr_memory) {
  119. ret = -ENOMEM;
  120. goto exit3;
  121. }
  122. if (!request_mem_region(p_dev->resource[3]->start,
  123. resource_size(p_dev->resource[3]),
  124. IPWIRELESS_PCCARD_NAME)) {
  125. ret = -EBUSY;
  126. goto exit4;
  127. }
  128. return 0;
  129. exit4:
  130. iounmap(ipw->attr_memory);
  131. exit3:
  132. release_mem_region(p_dev->resource[2]->start,
  133. resource_size(p_dev->resource[2]));
  134. exit2:
  135. iounmap(ipw->common_memory);
  136. exit1:
  137. release_region(p_dev->resource[0]->start,
  138. resource_size(p_dev->resource[0]));
  139. exit:
  140. pcmcia_disable_device(p_dev);
  141. return ret;
  142. }
  143. static int config_ipwireless(struct ipw_dev *ipw)
  144. {
  145. struct pcmcia_device *link = ipw->link;
  146. int ret = 0;
  147. ipw->is_v2_card = 0;
  148. link->config_flags |= CONF_AUTO_SET_IO | CONF_AUTO_SET_IOMEM |
  149. CONF_ENABLE_IRQ;
  150. ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
  151. if (ret != 0)
  152. return ret;
  153. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  154. ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
  155. ipw->attr_memory, ipw->common_memory,
  156. ipw->is_v2_card, signalled_reboot_callback,
  157. ipw);
  158. ret = pcmcia_request_irq(link, ipwireless_interrupt);
  159. if (ret != 0)
  160. goto exit;
  161. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  162. ipw->is_v2_card ? "V2/V3" : "V1");
  163. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  164. ": I/O ports %pR, irq %d\n", link->resource[0],
  165. (unsigned int) link->irq);
  166. if (ipw->attr_memory && ipw->common_memory)
  167. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  168. ": attr memory %pR, common memory %pR\n",
  169. link->resource[3],
  170. link->resource[2]);
  171. ipw->network = ipwireless_network_create(ipw->hardware);
  172. if (!ipw->network)
  173. goto exit;
  174. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
  175. if (!ipw->tty)
  176. goto exit;
  177. ipwireless_init_hardware_v2_v3(ipw->hardware);
  178. /*
  179. * Do the RequestConfiguration last, because it enables interrupts.
  180. * Then we don't get any interrupts before we're ready for them.
  181. */
  182. ret = pcmcia_enable_device(link);
  183. if (ret != 0)
  184. goto exit;
  185. return 0;
  186. exit:
  187. if (ipw->common_memory) {
  188. release_mem_region(link->resource[2]->start,
  189. resource_size(link->resource[2]));
  190. iounmap(ipw->common_memory);
  191. }
  192. if (ipw->attr_memory) {
  193. release_mem_region(link->resource[3]->start,
  194. resource_size(link->resource[3]));
  195. iounmap(ipw->attr_memory);
  196. }
  197. pcmcia_disable_device(link);
  198. return -1;
  199. }
  200. static void release_ipwireless(struct ipw_dev *ipw)
  201. {
  202. release_region(ipw->link->resource[0]->start,
  203. resource_size(ipw->link->resource[0]));
  204. if (ipw->common_memory) {
  205. release_mem_region(ipw->link->resource[2]->start,
  206. resource_size(ipw->link->resource[2]));
  207. iounmap(ipw->common_memory);
  208. }
  209. if (ipw->attr_memory) {
  210. release_mem_region(ipw->link->resource[3]->start,
  211. resource_size(ipw->link->resource[3]));
  212. iounmap(ipw->attr_memory);
  213. }
  214. pcmcia_disable_device(ipw->link);
  215. }
  216. /*
  217. * ipwireless_attach() creates an "instance" of the driver, allocating
  218. * local data structures for one device (one interface). The device
  219. * is registered with Card Services.
  220. *
  221. * The pcmcia_device structure is initialized, but we don't actually
  222. * configure the card at this point -- we wait until we receive a
  223. * card insertion event.
  224. */
  225. static int ipwireless_attach(struct pcmcia_device *link)
  226. {
  227. struct ipw_dev *ipw;
  228. int ret;
  229. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  230. if (!ipw)
  231. return -ENOMEM;
  232. ipw->link = link;
  233. link->priv = ipw;
  234. ipw->hardware = ipwireless_hardware_create();
  235. if (!ipw->hardware) {
  236. kfree(ipw);
  237. return -ENOMEM;
  238. }
  239. /* RegisterClient will call config_ipwireless */
  240. ret = config_ipwireless(ipw);
  241. if (ret != 0) {
  242. ipwireless_detach(link);
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. /*
  248. * This deletes a driver "instance". The device is de-registered with
  249. * Card Services. If it has been released, all local data structures
  250. * are freed. Otherwise, the structures will be freed when the device
  251. * is released.
  252. */
  253. static void ipwireless_detach(struct pcmcia_device *link)
  254. {
  255. struct ipw_dev *ipw = link->priv;
  256. release_ipwireless(ipw);
  257. if (ipw->tty != NULL)
  258. ipwireless_tty_free(ipw->tty);
  259. if (ipw->network != NULL)
  260. ipwireless_network_free(ipw->network);
  261. if (ipw->hardware != NULL)
  262. ipwireless_hardware_free(ipw->hardware);
  263. kfree(ipw);
  264. }
  265. static struct pcmcia_driver me = {
  266. .owner = THIS_MODULE,
  267. .probe = ipwireless_attach,
  268. .remove = ipwireless_detach,
  269. .name = IPWIRELESS_PCCARD_NAME,
  270. .id_table = ipw_ids
  271. };
  272. /*
  273. * Module insertion : initialisation of the module.
  274. * Register the card with cardmgr...
  275. */
  276. static int __init init_ipwireless(void)
  277. {
  278. int ret;
  279. ret = ipwireless_tty_init();
  280. if (ret != 0)
  281. return ret;
  282. ret = pcmcia_register_driver(&me);
  283. if (ret != 0)
  284. ipwireless_tty_release();
  285. return ret;
  286. }
  287. /*
  288. * Module removal
  289. */
  290. static void __exit exit_ipwireless(void)
  291. {
  292. pcmcia_unregister_driver(&me);
  293. ipwireless_tty_release();
  294. }
  295. module_init(init_ipwireless);
  296. module_exit(exit_ipwireless);
  297. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  298. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  299. MODULE_LICENSE("GPL");