main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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/version.h>
  29. #include <pcmcia/cisreg.h>
  30. #include <pcmcia/device_id.h>
  31. #include <pcmcia/ss.h>
  32. #include <pcmcia/ds.h>
  33. #include <pcmcia/cs.h>
  34. static struct pcmcia_device_id ipw_ids[] = {
  35. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
  36. PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
  37. PCMCIA_DEVICE_NULL
  38. };
  39. MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
  40. static void ipwireless_detach(struct pcmcia_device *link);
  41. /*
  42. * Module params
  43. */
  44. /* Debug mode: more verbose, print sent/recv bytes */
  45. int ipwireless_debug;
  46. int ipwireless_loopback;
  47. int ipwireless_out_queue = 1;
  48. module_param_named(debug, ipwireless_debug, int, 0);
  49. module_param_named(loopback, ipwireless_loopback, int, 0);
  50. module_param_named(out_queue, ipwireless_out_queue, int, 0);
  51. MODULE_PARM_DESC(debug, "switch on debug messages [0]");
  52. MODULE_PARM_DESC(loopback,
  53. "debug: enable ras_raw channel [0]");
  54. MODULE_PARM_DESC(out_queue, "debug: set size of outgoing queue [1]");
  55. /* Executes in process context. */
  56. static void signalled_reboot_work(struct work_struct *work_reboot)
  57. {
  58. struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
  59. work_reboot);
  60. struct pcmcia_device *link = ipw->link;
  61. int ret = pccard_reset_card(link->socket);
  62. if (ret != CS_SUCCESS)
  63. cs_error(link, ResetCard, ret);
  64. }
  65. static void signalled_reboot_callback(void *callback_data)
  66. {
  67. struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
  68. /* Delegate to process context. */
  69. schedule_work(&ipw->work_reboot);
  70. }
  71. static int config_ipwireless(struct ipw_dev *ipw)
  72. {
  73. struct pcmcia_device *link = ipw->link;
  74. int ret;
  75. config_info_t conf;
  76. tuple_t tuple;
  77. unsigned short buf[64];
  78. cisparse_t parse;
  79. unsigned short cor_value;
  80. win_req_t request_attr_memory;
  81. win_req_t request_common_memory;
  82. memreq_t memreq_attr_memory;
  83. memreq_t memreq_common_memory;
  84. ipw->is_v2_card = 0;
  85. tuple.Attributes = 0;
  86. tuple.TupleData = (cisdata_t *) buf;
  87. tuple.TupleDataMax = sizeof(buf);
  88. tuple.TupleOffset = 0;
  89. tuple.DesiredTuple = RETURN_FIRST_TUPLE;
  90. ret = pcmcia_get_first_tuple(link, &tuple);
  91. while (ret == 0) {
  92. ret = pcmcia_get_tuple_data(link, &tuple);
  93. if (ret != CS_SUCCESS) {
  94. cs_error(link, GetTupleData, ret);
  95. goto exit0;
  96. }
  97. ret = pcmcia_get_next_tuple(link, &tuple);
  98. }
  99. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  100. ret = pcmcia_get_first_tuple(link, &tuple);
  101. if (ret != CS_SUCCESS) {
  102. cs_error(link, GetFirstTuple, ret);
  103. goto exit0;
  104. }
  105. ret = pcmcia_get_tuple_data(link, &tuple);
  106. if (ret != CS_SUCCESS) {
  107. cs_error(link, GetTupleData, ret);
  108. goto exit0;
  109. }
  110. ret = pcmcia_parse_tuple(link, &tuple, &parse);
  111. if (ret != CS_SUCCESS) {
  112. cs_error(link, ParseTuple, ret);
  113. goto exit0;
  114. }
  115. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  116. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  117. link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
  118. link->io.IOAddrLines = 16;
  119. link->irq.IRQInfo1 = parse.cftable_entry.irq.IRQInfo1;
  120. /* 0x40 causes it to generate level mode interrupts. */
  121. /* 0x04 enables IREQ pin. */
  122. cor_value = parse.cftable_entry.index | 0x44;
  123. link->conf.ConfigIndex = cor_value;
  124. /* IRQ and I/O settings */
  125. tuple.DesiredTuple = CISTPL_CONFIG;
  126. ret = pcmcia_get_first_tuple(link, &tuple);
  127. if (ret != CS_SUCCESS) {
  128. cs_error(link, GetFirstTuple, ret);
  129. goto exit0;
  130. }
  131. ret = pcmcia_get_tuple_data(link, &tuple);
  132. if (ret != CS_SUCCESS) {
  133. cs_error(link, GetTupleData, ret);
  134. goto exit0;
  135. }
  136. ret = pcmcia_parse_tuple(link, &tuple, &parse);
  137. if (ret != CS_SUCCESS) {
  138. cs_error(link, GetTupleData, ret);
  139. goto exit0;
  140. }
  141. link->conf.Attributes = CONF_ENABLE_IRQ;
  142. link->conf.ConfigBase = parse.config.base;
  143. link->conf.Present = parse.config.rmask[0];
  144. link->conf.IntType = INT_MEMORY_AND_IO;
  145. link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
  146. link->irq.Handler = ipwireless_interrupt;
  147. link->irq.Instance = ipw->hardware;
  148. ret = pcmcia_request_io(link, &link->io);
  149. if (ret != CS_SUCCESS) {
  150. cs_error(link, RequestIO, ret);
  151. goto exit0;
  152. }
  153. /* memory settings */
  154. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  155. ret = pcmcia_get_first_tuple(link, &tuple);
  156. if (ret != CS_SUCCESS) {
  157. cs_error(link, GetFirstTuple, ret);
  158. goto exit1;
  159. }
  160. ret = pcmcia_get_tuple_data(link, &tuple);
  161. if (ret != CS_SUCCESS) {
  162. cs_error(link, GetTupleData, ret);
  163. goto exit1;
  164. }
  165. ret = pcmcia_parse_tuple(link, &tuple, &parse);
  166. if (ret != CS_SUCCESS) {
  167. cs_error(link, ParseTuple, ret);
  168. goto exit1;
  169. }
  170. if (parse.cftable_entry.mem.nwin > 0) {
  171. request_common_memory.Attributes =
  172. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
  173. request_common_memory.Base =
  174. parse.cftable_entry.mem.win[0].host_addr;
  175. request_common_memory.Size = parse.cftable_entry.mem.win[0].len;
  176. if (request_common_memory.Size < 0x1000)
  177. request_common_memory.Size = 0x1000;
  178. request_common_memory.AccessSpeed = 0;
  179. ret = pcmcia_request_window(&link, &request_common_memory,
  180. &ipw->handle_common_memory);
  181. if (ret != CS_SUCCESS) {
  182. cs_error(link, RequestWindow, ret);
  183. goto exit1;
  184. }
  185. memreq_common_memory.CardOffset =
  186. parse.cftable_entry.mem.win[0].card_addr;
  187. memreq_common_memory.Page = 0;
  188. ret = pcmcia_map_mem_page(ipw->handle_common_memory,
  189. &memreq_common_memory);
  190. if (ret != CS_SUCCESS) {
  191. cs_error(link, MapMemPage, ret);
  192. goto exit1;
  193. }
  194. ipw->is_v2_card =
  195. parse.cftable_entry.mem.win[0].len == 0x100;
  196. ipw->common_memory = ioremap(request_common_memory.Base,
  197. request_common_memory.Size);
  198. request_attr_memory.Attributes =
  199. WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
  200. request_attr_memory.Base = 0;
  201. request_attr_memory.Size = 0; /* this used to be 0x1000 */
  202. request_attr_memory.AccessSpeed = 0;
  203. ret = pcmcia_request_window(&link, &request_attr_memory,
  204. &ipw->handle_attr_memory);
  205. if (ret != CS_SUCCESS) {
  206. cs_error(link, RequestWindow, ret);
  207. goto exit2;
  208. }
  209. memreq_attr_memory.CardOffset = 0;
  210. memreq_attr_memory.Page = 0;
  211. ret = pcmcia_map_mem_page(ipw->handle_attr_memory,
  212. &memreq_attr_memory);
  213. if (ret != CS_SUCCESS) {
  214. cs_error(link, MapMemPage, ret);
  215. goto exit2;
  216. }
  217. ipw->attr_memory = ioremap(request_attr_memory.Base,
  218. request_attr_memory.Size);
  219. }
  220. INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
  221. ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
  222. ipw->attr_memory, ipw->common_memory,
  223. ipw->is_v2_card, signalled_reboot_callback,
  224. ipw);
  225. ret = pcmcia_request_irq(link, &link->irq);
  226. if (ret != CS_SUCCESS) {
  227. cs_error(link, RequestIRQ, ret);
  228. goto exit3;
  229. }
  230. /* Look up current Vcc */
  231. ret = pcmcia_get_configuration_info(link, &conf);
  232. if (ret != CS_SUCCESS) {
  233. cs_error(link, GetConfigurationInfo, ret);
  234. goto exit4;
  235. }
  236. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
  237. ipw->is_v2_card ? "V2/V3" : "V1");
  238. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  239. ": I/O ports 0x%04x-0x%04x, irq %d\n",
  240. (unsigned int) link->io.BasePort1,
  241. (unsigned int) (link->io.BasePort1 +
  242. link->io.NumPorts1 - 1),
  243. (unsigned int) link->irq.AssignedIRQ);
  244. if (ipw->attr_memory && ipw->common_memory)
  245. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  246. ": attr memory 0x%08lx-0x%08lx, "
  247. "common memory 0x%08lx-0x%08lx\n",
  248. request_attr_memory.Base,
  249. request_attr_memory.Base
  250. + request_attr_memory.Size - 1,
  251. request_common_memory.Base,
  252. request_common_memory.Base
  253. + request_common_memory.Size - 1);
  254. ipw->network = ipwireless_network_create(ipw->hardware);
  255. if (!ipw->network)
  256. goto exit3;
  257. ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
  258. ipw->nodes);
  259. if (!ipw->tty)
  260. goto exit3;
  261. ipwireless_init_hardware_v2_v3(ipw->hardware);
  262. /*
  263. * Do the RequestConfiguration last, because it enables interrupts.
  264. * Then we don't get any interrupts before we're ready for them.
  265. */
  266. ret = pcmcia_request_configuration(link, &link->conf);
  267. if (ret != CS_SUCCESS) {
  268. cs_error(link, RequestConfiguration, ret);
  269. goto exit4;
  270. }
  271. link->dev_node = &ipw->nodes[0];
  272. return 0;
  273. exit4:
  274. pcmcia_disable_device(link);
  275. exit3:
  276. if (ipw->attr_memory) {
  277. iounmap(ipw->attr_memory);
  278. pcmcia_release_window(ipw->handle_attr_memory);
  279. pcmcia_disable_device(link);
  280. }
  281. exit2:
  282. if (ipw->common_memory) {
  283. iounmap(ipw->common_memory);
  284. pcmcia_release_window(ipw->handle_common_memory);
  285. }
  286. exit1:
  287. pcmcia_disable_device(link);
  288. exit0:
  289. return -1;
  290. }
  291. static void release_ipwireless(struct ipw_dev *ipw)
  292. {
  293. struct pcmcia_device *link = ipw->link;
  294. pcmcia_disable_device(link);
  295. if (ipw->common_memory)
  296. iounmap(ipw->common_memory);
  297. if (ipw->attr_memory)
  298. iounmap(ipw->attr_memory);
  299. if (ipw->common_memory)
  300. pcmcia_release_window(ipw->handle_common_memory);
  301. if (ipw->attr_memory)
  302. pcmcia_release_window(ipw->handle_attr_memory);
  303. pcmcia_disable_device(link);
  304. }
  305. /*
  306. * ipwireless_attach() creates an "instance" of the driver, allocating
  307. * local data structures for one device (one interface). The device
  308. * is registered with Card Services.
  309. *
  310. * The pcmcia_device structure is initialized, but we don't actually
  311. * configure the card at this point -- we wait until we receive a
  312. * card insertion event.
  313. */
  314. static int ipwireless_attach(struct pcmcia_device *link)
  315. {
  316. struct ipw_dev *ipw;
  317. int ret;
  318. ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
  319. if (!ipw)
  320. return -ENOMEM;
  321. ipw->link = link;
  322. link->priv = ipw;
  323. link->irq.Instance = ipw;
  324. /* Link this device into our device list. */
  325. link->dev_node = &ipw->nodes[0];
  326. ipw->hardware = ipwireless_hardware_create();
  327. if (!ipw->hardware) {
  328. kfree(ipw);
  329. return -ENOMEM;
  330. }
  331. /* RegisterClient will call config_ipwireless */
  332. ret = config_ipwireless(ipw);
  333. if (ret != 0) {
  334. cs_error(link, RegisterClient, ret);
  335. ipwireless_detach(link);
  336. return ret;
  337. }
  338. return 0;
  339. }
  340. /*
  341. * This deletes a driver "instance". The device is de-registered with
  342. * Card Services. If it has been released, all local data structures
  343. * are freed. Otherwise, the structures will be freed when the device
  344. * is released.
  345. */
  346. static void ipwireless_detach(struct pcmcia_device *link)
  347. {
  348. struct ipw_dev *ipw = link->priv;
  349. release_ipwireless(ipw);
  350. /* Break the link with Card Services */
  351. if (link)
  352. pcmcia_disable_device(link);
  353. if (ipw->tty != NULL)
  354. ipwireless_tty_free(ipw->tty);
  355. if (ipw->network != NULL)
  356. ipwireless_network_free(ipw->network);
  357. if (ipw->hardware != NULL)
  358. ipwireless_hardware_free(ipw->hardware);
  359. kfree(ipw);
  360. }
  361. static struct pcmcia_driver me = {
  362. .owner = THIS_MODULE,
  363. .probe = ipwireless_attach,
  364. .remove = ipwireless_detach,
  365. .drv = { .name = IPWIRELESS_PCCARD_NAME },
  366. .id_table = ipw_ids
  367. };
  368. /*
  369. * Module insertion : initialisation of the module.
  370. * Register the card with cardmgr...
  371. */
  372. static int __init init_ipwireless(void)
  373. {
  374. int ret;
  375. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  376. IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
  377. ret = ipwireless_tty_init();
  378. if (ret != 0)
  379. return ret;
  380. ret = pcmcia_register_driver(&me);
  381. if (ret != 0)
  382. ipwireless_tty_release();
  383. return ret;
  384. }
  385. /*
  386. * Module removal
  387. */
  388. static void __exit exit_ipwireless(void)
  389. {
  390. printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
  391. IPWIRELESS_PCMCIA_VERSION " removed\n");
  392. pcmcia_unregister_driver(&me);
  393. ipwireless_tty_release();
  394. }
  395. module_init(init_ipwireless);
  396. module_exit(exit_ipwireless);
  397. MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
  398. MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
  399. MODULE_LICENSE("GPL");