main.c 8.8 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. #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. int ret;
  77. p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
  78. p_dev->resource[0]->start = cfg->io.win[0].base;
  79. p_dev->resource[0]->end = cfg->io.win[0].len;
  80. /* 0x40 causes it to generate level mode interrupts. */
  81. /* 0x04 enables IREQ pin. */
  82. p_dev->config_index = cfg->index | 0x44;
  83. p_dev->io_lines = 16;
  84. ret = pcmcia_request_io(p_dev);
  85. if (ret)
  86. return ret;
  87. io_resource = request_region(p_dev->resource[0]->start,
  88. resource_size(p_dev->resource[0]),
  89. IPWIRELESS_PCCARD_NAME);
  90. if (cfg->mem.nwin == 0)
  91. return 0;
  92. p_dev->resource[2]->flags |=
  93. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  94. p_dev->resource[2]->start = cfg->mem.win[0].host_addr;
  95. p_dev->resource[2]->end = cfg->mem.win[0].len;
  96. if (p_dev->resource[2]->end < 0x1000)
  97. p_dev->resource[2]->end = 0x1000;
  98. ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
  99. if (ret != 0)
  100. goto exit1;
  101. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2],
  102. cfg->mem.win[0].card_addr);
  103. if (ret != 0)
  104. goto exit2;
  105. ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
  106. ipw->attr_memory = ioremap(p_dev->resource[2]->start,
  107. resource_size(p_dev->resource[2]));
  108. request_mem_region(p_dev->resource[2]->start,
  109. resource_size(p_dev->resource[2]),
  110. IPWIRELESS_PCCARD_NAME);
  111. p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
  112. WIN_ENABLE;
  113. p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
  114. ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
  115. if (ret != 0)
  116. goto exit2;
  117. ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
  118. if (ret != 0)
  119. goto exit3;
  120. ipw->attr_memory = ioremap(p_dev->resource[3]->start,
  121. resource_size(p_dev->resource[3]));
  122. request_mem_region(p_dev->resource[3]->start,
  123. resource_size(p_dev->resource[3]),
  124. IPWIRELESS_PCCARD_NAME);
  125. return 0;
  126. exit3:
  127. exit2:
  128. if (ipw->common_memory) {
  129. release_mem_region(p_dev->resource[2]->start,
  130. resource_size(p_dev->resource[2]));
  131. iounmap(ipw->common_memory);
  132. }
  133. exit1:
  134. release_resource(io_resource);
  135. pcmcia_disable_device(p_dev);
  136. return -1;
  137. }
  138. static int config_ipwireless(struct ipw_dev *ipw)
  139. {
  140. struct pcmcia_device *link = ipw->link;
  141. int ret = 0;
  142. ipw->is_v2_card = 0;
  143. ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
  144. if (ret != 0)
  145. return ret;
  146. link->conf.Attributes = CONF_ENABLE_IRQ;
  147. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  148. ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
  149. ipw->attr_memory, ipw->common_memory,
  150. ipw->is_v2_card, signalled_reboot_callback,
  151. ipw);
  152. ret = pcmcia_request_irq(link, ipwireless_interrupt);
  153. if (ret != 0)
  154. goto exit;
  155. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  156. ipw->is_v2_card ? "V2/V3" : "V1");
  157. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  158. ": I/O ports %pR, irq %d\n", link->resource[0],
  159. (unsigned int) link->irq);
  160. if (ipw->attr_memory && ipw->common_memory)
  161. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  162. ": attr memory %pR, common memory %pR\n",
  163. link->resource[3],
  164. link->resource[2]);
  165. ipw->network = ipwireless_network_create(ipw->hardware);
  166. if (!ipw->network)
  167. goto exit;
  168. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
  169. if (!ipw->tty)
  170. goto exit;
  171. ipwireless_init_hardware_v2_v3(ipw->hardware);
  172. /*
  173. * Do the RequestConfiguration last, because it enables interrupts.
  174. * Then we don't get any interrupts before we're ready for them.
  175. */
  176. ret = pcmcia_request_configuration(link, &link->conf);
  177. if (ret != 0)
  178. goto exit;
  179. return 0;
  180. exit:
  181. if (ipw->common_memory) {
  182. release_mem_region(link->resource[2]->start,
  183. resource_size(link->resource[2]));
  184. iounmap(ipw->common_memory);
  185. }
  186. if (ipw->attr_memory) {
  187. release_mem_region(link->resource[3]->start,
  188. resource_size(link->resource[3]));
  189. iounmap(ipw->attr_memory);
  190. }
  191. pcmcia_disable_device(link);
  192. return -1;
  193. }
  194. static void release_ipwireless(struct ipw_dev *ipw)
  195. {
  196. if (ipw->common_memory) {
  197. release_mem_region(ipw->link->resource[2]->start,
  198. resource_size(ipw->link->resource[2]));
  199. iounmap(ipw->common_memory);
  200. }
  201. if (ipw->attr_memory) {
  202. release_mem_region(ipw->link->resource[3]->start,
  203. resource_size(ipw->link->resource[3]));
  204. iounmap(ipw->attr_memory);
  205. }
  206. pcmcia_disable_device(ipw->link);
  207. }
  208. /*
  209. * ipwireless_attach() creates an "instance" of the driver, allocating
  210. * local data structures for one device (one interface). The device
  211. * is registered with Card Services.
  212. *
  213. * The pcmcia_device structure is initialized, but we don't actually
  214. * configure the card at this point -- we wait until we receive a
  215. * card insertion event.
  216. */
  217. static int ipwireless_attach(struct pcmcia_device *link)
  218. {
  219. struct ipw_dev *ipw;
  220. int ret;
  221. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  222. if (!ipw)
  223. return -ENOMEM;
  224. ipw->link = link;
  225. link->priv = ipw;
  226. ipw->hardware = ipwireless_hardware_create();
  227. if (!ipw->hardware) {
  228. kfree(ipw);
  229. return -ENOMEM;
  230. }
  231. /* RegisterClient will call config_ipwireless */
  232. ret = config_ipwireless(ipw);
  233. if (ret != 0) {
  234. ipwireless_detach(link);
  235. return ret;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * This deletes a driver "instance". The device is de-registered with
  241. * Card Services. If it has been released, all local data structures
  242. * are freed. Otherwise, the structures will be freed when the device
  243. * is released.
  244. */
  245. static void ipwireless_detach(struct pcmcia_device *link)
  246. {
  247. struct ipw_dev *ipw = link->priv;
  248. release_ipwireless(ipw);
  249. if (ipw->tty != NULL)
  250. ipwireless_tty_free(ipw->tty);
  251. if (ipw->network != NULL)
  252. ipwireless_network_free(ipw->network);
  253. if (ipw->hardware != NULL)
  254. ipwireless_hardware_free(ipw->hardware);
  255. kfree(ipw);
  256. }
  257. static struct pcmcia_driver me = {
  258. .owner = THIS_MODULE,
  259. .probe = ipwireless_attach,
  260. .remove = ipwireless_detach,
  261. .drv = { .name = IPWIRELESS_PCCARD_NAME },
  262. .id_table = ipw_ids
  263. };
  264. /*
  265. * Module insertion : initialisation of the module.
  266. * Register the card with cardmgr...
  267. */
  268. static int __init init_ipwireless(void)
  269. {
  270. int ret;
  271. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  272. IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
  273. ret = ipwireless_tty_init();
  274. if (ret != 0)
  275. return ret;
  276. ret = pcmcia_register_driver(&me);
  277. if (ret != 0)
  278. ipwireless_tty_release();
  279. return ret;
  280. }
  281. /*
  282. * Module removal
  283. */
  284. static void __exit exit_ipwireless(void)
  285. {
  286. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  287. IPWIRELESS_PCMCIA_VERSION " removed\n");
  288. pcmcia_unregister_driver(&me);
  289. ipwireless_tty_release();
  290. }
  291. module_init(init_ipwireless);
  292. module_exit(exit_ipwireless);
  293. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  294. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  295. MODULE_LICENSE("GPL");