GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / misc / iwmc3200top / main.c
blob654d4ac2c5a28b1baa26de7fa1277d3a8d3c1cc3
1 /*
2 * iwmc3200top - Intel Wireless MultiCom 3200 Top Driver
3 * drivers/misc/iwmc3200top/main.c
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
22 * Author Name: Maxim Grabarnik <maxim.grabarnink@intel.com>
23 * -
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/debugfs.h>
32 #include <linux/mmc/sdio_ids.h>
33 #include <linux/mmc/sdio_func.h>
34 #include <linux/mmc/sdio.h>
36 #include "iwmc3200top.h"
37 #include "log.h"
38 #include "fw-msg.h"
39 #include "debugfs.h"
42 #define DRIVER_DESCRIPTION "Intel(R) IWMC 3200 Top Driver"
43 #define DRIVER_COPYRIGHT "Copyright (c) 2008 Intel Corporation."
45 #define DRIVER_VERSION "0.1.62"
47 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
48 MODULE_VERSION(DRIVER_VERSION);
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR(DRIVER_COPYRIGHT);
51 MODULE_FIRMWARE(FW_NAME(FW_API_VER));
54 static inline int __iwmct_tx(struct iwmct_priv *priv, void *src, int count)
56 return sdio_memcpy_toio(priv->func, IWMC_SDIO_DATA_ADDR, src, count);
59 int iwmct_tx(struct iwmct_priv *priv, void *src, int count)
61 int ret;
62 sdio_claim_host(priv->func);
63 ret = __iwmct_tx(priv, src, count);
64 sdio_release_host(priv->func);
65 return ret;
68 * This workers main task is to wait for OP_OPR_ALIVE
69 * from TOP FW until ALIVE_MSG_TIMOUT timeout is elapsed.
70 * When OP_OPR_ALIVE received it will issue
71 * a call to "bus_rescan_devices".
73 static void iwmct_rescan_worker(struct work_struct *ws)
75 struct iwmct_priv *priv;
76 int ret;
78 priv = container_of(ws, struct iwmct_priv, bus_rescan_worker);
80 LOG_INFO(priv, FW_MSG, "Calling bus_rescan\n");
82 ret = bus_rescan_devices(priv->func->dev.bus);
83 if (ret < 0)
84 LOG_INFO(priv, INIT, "bus_rescan_devices FAILED!!!\n");
87 static void op_top_message(struct iwmct_priv *priv, struct top_msg *msg)
89 switch (msg->hdr.opcode) {
90 case OP_OPR_ALIVE:
91 LOG_INFO(priv, FW_MSG, "Got ALIVE from device, wake rescan\n");
92 queue_work(priv->bus_rescan_wq, &priv->bus_rescan_worker);
93 break;
94 default:
95 LOG_INFO(priv, FW_MSG, "Received msg opcode 0x%X\n",
96 msg->hdr.opcode);
97 break;
102 static void handle_top_message(struct iwmct_priv *priv, u8 *buf, int len)
104 struct top_msg *msg;
106 msg = (struct top_msg *)buf;
108 if (msg->hdr.type != COMM_TYPE_D2H) {
109 LOG_ERROR(priv, FW_MSG,
110 "Message from TOP with invalid message type 0x%X\n",
111 msg->hdr.type);
112 return;
115 if (len < sizeof(msg->hdr)) {
116 LOG_ERROR(priv, FW_MSG,
117 "Message from TOP is too short for message header "
118 "received %d bytes, expected at least %zd bytes\n",
119 len, sizeof(msg->hdr));
120 return;
123 if (len < le16_to_cpu(msg->hdr.length) + sizeof(msg->hdr)) {
124 LOG_ERROR(priv, FW_MSG,
125 "Message length (%d bytes) is shorter than "
126 "in header (%d bytes)\n",
127 len, le16_to_cpu(msg->hdr.length));
128 return;
131 switch (msg->hdr.category) {
132 case COMM_CATEGORY_OPERATIONAL:
133 op_top_message(priv, (struct top_msg *)buf);
134 break;
136 case COMM_CATEGORY_DEBUG:
137 case COMM_CATEGORY_TESTABILITY:
138 case COMM_CATEGORY_DIAGNOSTICS:
139 iwmct_log_top_message(priv, buf, len);
140 break;
142 default:
143 LOG_ERROR(priv, FW_MSG,
144 "Message from TOP with unknown category 0x%X\n",
145 msg->hdr.category);
146 break;
150 int iwmct_send_hcmd(struct iwmct_priv *priv, u8 *cmd, u16 len)
152 int ret;
153 u8 *buf;
155 LOG_TRACE(priv, FW_MSG, "Sending hcmd:\n");
157 /* add padding to 256 for IWMC */
158 ((struct top_msg *)cmd)->hdr.flags |= CMD_FLAG_PADDING_256;
160 LOG_HEXDUMP(FW_MSG, cmd, len);
162 if (len > FW_HCMD_BLOCK_SIZE) {
163 LOG_ERROR(priv, FW_MSG, "size %d exceeded hcmd max size %d\n",
164 len, FW_HCMD_BLOCK_SIZE);
165 return -1;
168 buf = kzalloc(FW_HCMD_BLOCK_SIZE, GFP_KERNEL);
169 if (!buf) {
170 LOG_ERROR(priv, FW_MSG, "kzalloc error, buf size %d\n",
171 FW_HCMD_BLOCK_SIZE);
172 return -1;
175 memcpy(buf, cmd, len);
176 ret = iwmct_tx(priv, buf, FW_HCMD_BLOCK_SIZE);
178 kfree(buf);
179 return ret;
183 static void iwmct_irq_read_worker(struct work_struct *ws)
185 struct iwmct_priv *priv;
186 struct iwmct_work_struct *read_req;
187 __le32 *buf = NULL;
188 int ret;
189 int iosize;
190 u32 barker;
191 bool is_barker;
193 priv = container_of(ws, struct iwmct_priv, isr_worker);
195 LOG_TRACE(priv, IRQ, "enter iwmct_irq_read_worker %p\n", ws);
197 /* --------------------- Handshake with device -------------------- */
198 sdio_claim_host(priv->func);
200 /* all list manipulations have to be protected by
201 * sdio_claim_host/sdio_release_host */
202 if (list_empty(&priv->read_req_list)) {
203 LOG_ERROR(priv, IRQ, "read_req_list empty in read worker\n");
204 goto exit_release;
207 read_req = list_entry(priv->read_req_list.next,
208 struct iwmct_work_struct, list);
210 list_del(&read_req->list);
211 iosize = read_req->iosize;
212 kfree(read_req);
214 buf = kzalloc(iosize, GFP_KERNEL);
215 if (!buf) {
216 LOG_ERROR(priv, IRQ, "kzalloc error, buf size %d\n", iosize);
217 goto exit_release;
220 LOG_INFO(priv, IRQ, "iosize=%d, buf=%p, func=%d\n",
221 iosize, buf, priv->func->num);
223 /* read from device */
224 ret = sdio_memcpy_fromio(priv->func, buf, IWMC_SDIO_DATA_ADDR, iosize);
225 if (ret) {
226 LOG_ERROR(priv, IRQ, "error %d reading buffer\n", ret);
227 goto exit_release;
230 LOG_HEXDUMP(IRQ, (u8 *)buf, iosize);
232 barker = le32_to_cpu(buf[0]);
234 /* Verify whether it's a barker and if not - treat as regular Rx */
235 if (barker == IWMC_BARKER_ACK ||
236 (barker & BARKER_DNLOAD_BARKER_MSK) == IWMC_BARKER_REBOOT) {
238 /* Valid Barker is equal on first 4 dwords */
239 is_barker = (buf[1] == buf[0]) &&
240 (buf[2] == buf[0]) &&
241 (buf[3] == buf[0]);
243 if (!is_barker) {
244 LOG_WARNING(priv, IRQ,
245 "Potentially inconsistent barker "
246 "%08X_%08X_%08X_%08X\n",
247 le32_to_cpu(buf[0]), le32_to_cpu(buf[1]),
248 le32_to_cpu(buf[2]), le32_to_cpu(buf[3]));
250 } else {
251 is_barker = false;
254 /* Handle Top CommHub message */
255 if (!is_barker) {
256 sdio_release_host(priv->func);
257 handle_top_message(priv, (u8 *)buf, iosize);
258 goto exit;
259 } else if (barker == IWMC_BARKER_ACK) { /* Handle barkers */
260 if (atomic_read(&priv->dev_sync) == 0) {
261 LOG_ERROR(priv, IRQ,
262 "ACK barker arrived out-of-sync\n");
263 goto exit_release;
266 /* Continuing to FW download (after Sync is completed)*/
267 atomic_set(&priv->dev_sync, 0);
268 LOG_INFO(priv, IRQ, "ACK barker arrived "
269 "- starting FW download\n");
270 } else { /* REBOOT barker */
271 LOG_INFO(priv, IRQ, "Recieved reboot barker: %x\n", barker);
272 priv->barker = barker;
274 if (barker & BARKER_DNLOAD_SYNC_MSK) {
275 /* Send the same barker back */
276 ret = __iwmct_tx(priv, buf, iosize);
277 if (ret) {
278 LOG_ERROR(priv, IRQ,
279 "error %d echoing barker\n", ret);
280 goto exit_release;
282 LOG_INFO(priv, IRQ, "Echoing barker to device\n");
283 atomic_set(&priv->dev_sync, 1);
284 goto exit_release;
287 /* Continuing to FW download (without Sync) */
288 LOG_INFO(priv, IRQ, "No sync requested "
289 "- starting FW download\n");
292 sdio_release_host(priv->func);
294 if (priv->dbg.fw_download)
295 iwmct_fw_load(priv);
296 else
297 LOG_ERROR(priv, IRQ, "FW download not allowed\n");
299 goto exit;
301 exit_release:
302 sdio_release_host(priv->func);
303 exit:
304 kfree(buf);
305 LOG_TRACE(priv, IRQ, "exit iwmct_irq_read_worker\n");
308 static void iwmct_irq(struct sdio_func *func)
310 struct iwmct_priv *priv;
311 int val, ret;
312 int iosize;
313 int addr = IWMC_SDIO_INTR_GET_SIZE_ADDR;
314 struct iwmct_work_struct *read_req;
316 priv = sdio_get_drvdata(func);
318 LOG_TRACE(priv, IRQ, "enter iwmct_irq\n");
320 /* read the function's status register */
321 val = sdio_readb(func, IWMC_SDIO_INTR_STATUS_ADDR, &ret);
323 LOG_TRACE(priv, IRQ, "iir value = %d, ret=%d\n", val, ret);
325 if (!val) {
326 LOG_ERROR(priv, IRQ, "iir = 0, exiting ISR\n");
327 goto exit_clear_intr;
332 * read 2 bytes of the transaction size
333 * IMPORTANT: sdio transaction size has to be read before clearing
334 * sdio interrupt!!!
336 val = sdio_readb(priv->func, addr++, &ret);
337 iosize = val;
338 val = sdio_readb(priv->func, addr++, &ret);
339 iosize += val << 8;
341 LOG_INFO(priv, IRQ, "READ size %d\n", iosize);
343 if (iosize == 0) {
344 LOG_ERROR(priv, IRQ, "READ size %d, exiting ISR\n", iosize);
345 goto exit_clear_intr;
348 /* allocate a work structure to pass iosize to the worker */
349 read_req = kzalloc(sizeof(struct iwmct_work_struct), GFP_KERNEL);
350 if (!read_req) {
351 LOG_ERROR(priv, IRQ, "failed to allocate read_req, exit ISR\n");
352 goto exit_clear_intr;
355 INIT_LIST_HEAD(&read_req->list);
356 read_req->iosize = iosize;
358 list_add_tail(&priv->read_req_list, &read_req->list);
360 /* clear the function's interrupt request bit (write 1 to clear) */
361 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
363 queue_work(priv->wq, &priv->isr_worker);
365 LOG_TRACE(priv, IRQ, "exit iwmct_irq\n");
367 return;
369 exit_clear_intr:
370 /* clear the function's interrupt request bit (write 1 to clear) */
371 sdio_writeb(func, 1, IWMC_SDIO_INTR_CLEAR_ADDR, &ret);
375 static int blocks;
376 module_param(blocks, int, 0604);
377 MODULE_PARM_DESC(blocks, "max_blocks_to_send");
379 static int dump;
380 module_param(dump, bool, 0604);
381 MODULE_PARM_DESC(dump, "dump_hex_content");
383 static int jump = 1;
384 module_param(jump, bool, 0604);
386 static int direct = 1;
387 module_param(direct, bool, 0604);
389 static int checksum = 1;
390 module_param(checksum, bool, 0604);
392 static int fw_download = 1;
393 module_param(fw_download, bool, 0604);
395 static int block_size = IWMC_SDIO_BLK_SIZE;
396 module_param(block_size, int, 0404);
398 static int download_trans_blks = IWMC_DEFAULT_TR_BLK;
399 module_param(download_trans_blks, int, 0604);
401 static int rubbish_barker;
402 module_param(rubbish_barker, bool, 0604);
404 #ifdef CONFIG_IWMC3200TOP_DEBUG
405 static int log_level[LOG_SRC_MAX];
406 static unsigned int log_level_argc;
407 module_param_array(log_level, int, &log_level_argc, 0604);
408 MODULE_PARM_DESC(log_level, "log_level");
410 static int log_level_fw[FW_LOG_SRC_MAX];
411 static unsigned int log_level_fw_argc;
412 module_param_array(log_level_fw, int, &log_level_fw_argc, 0604);
413 MODULE_PARM_DESC(log_level_fw, "log_level_fw");
414 #endif
416 void iwmct_dbg_init_params(struct iwmct_priv *priv)
418 #ifdef CONFIG_IWMC3200TOP_DEBUG
419 int i;
421 for (i = 0; i < log_level_argc; i++) {
422 dev_notice(&priv->func->dev, "log_level[%d]=0x%X\n",
423 i, log_level[i]);
424 iwmct_log_set_filter((log_level[i] >> 8) & 0xFF,
425 log_level[i] & 0xFF);
427 for (i = 0; i < log_level_fw_argc; i++) {
428 dev_notice(&priv->func->dev, "log_level_fw[%d]=0x%X\n",
429 i, log_level_fw[i]);
430 iwmct_log_set_fw_filter((log_level_fw[i] >> 8) & 0xFF,
431 log_level_fw[i] & 0xFF);
433 #endif
435 priv->dbg.blocks = blocks;
436 LOG_INFO(priv, INIT, "blocks=%d\n", blocks);
437 priv->dbg.dump = (bool)dump;
438 LOG_INFO(priv, INIT, "dump=%d\n", dump);
439 priv->dbg.jump = (bool)jump;
440 LOG_INFO(priv, INIT, "jump=%d\n", jump);
441 priv->dbg.direct = (bool)direct;
442 LOG_INFO(priv, INIT, "direct=%d\n", direct);
443 priv->dbg.checksum = (bool)checksum;
444 LOG_INFO(priv, INIT, "checksum=%d\n", checksum);
445 priv->dbg.fw_download = (bool)fw_download;
446 LOG_INFO(priv, INIT, "fw_download=%d\n", fw_download);
447 priv->dbg.block_size = block_size;
448 LOG_INFO(priv, INIT, "block_size=%d\n", block_size);
449 priv->dbg.download_trans_blks = download_trans_blks;
450 LOG_INFO(priv, INIT, "download_trans_blks=%d\n", download_trans_blks);
453 /*****************************************************************************
455 * sysfs attributes
457 *****************************************************************************/
458 static ssize_t show_iwmct_fw_version(struct device *d,
459 struct device_attribute *attr, char *buf)
461 struct iwmct_priv *priv = dev_get_drvdata(d);
462 return sprintf(buf, "%s\n", priv->dbg.label_fw);
464 static DEVICE_ATTR(cc_label_fw, S_IRUGO, show_iwmct_fw_version, NULL);
466 #ifdef CONFIG_IWMC3200TOP_DEBUG
467 static DEVICE_ATTR(log_level, S_IWUSR | S_IRUGO,
468 show_iwmct_log_level, store_iwmct_log_level);
469 static DEVICE_ATTR(log_level_fw, S_IWUSR | S_IRUGO,
470 show_iwmct_log_level_fw, store_iwmct_log_level_fw);
471 #endif
473 static struct attribute *iwmct_sysfs_entries[] = {
474 &dev_attr_cc_label_fw.attr,
475 #ifdef CONFIG_IWMC3200TOP_DEBUG
476 &dev_attr_log_level.attr,
477 &dev_attr_log_level_fw.attr,
478 #endif
479 NULL
482 static struct attribute_group iwmct_attribute_group = {
483 .name = NULL, /* put in device directory */
484 .attrs = iwmct_sysfs_entries,
488 static int iwmct_probe(struct sdio_func *func,
489 const struct sdio_device_id *id)
491 struct iwmct_priv *priv;
492 int ret;
493 int val = 1;
494 int addr = IWMC_SDIO_INTR_ENABLE_ADDR;
496 dev_dbg(&func->dev, "enter iwmct_probe\n");
498 dev_dbg(&func->dev, "IRQ polling period id %u msecs, HZ is %d\n",
499 jiffies_to_msecs(2147483647), HZ);
501 priv = kzalloc(sizeof(struct iwmct_priv), GFP_KERNEL);
502 if (!priv) {
503 dev_err(&func->dev, "kzalloc error\n");
504 return -ENOMEM;
506 priv->func = func;
507 sdio_set_drvdata(func, priv);
510 /* create drivers work queue */
511 priv->wq = create_workqueue(DRV_NAME "_wq");
512 priv->bus_rescan_wq = create_workqueue(DRV_NAME "_rescan_wq");
513 INIT_WORK(&priv->bus_rescan_worker, iwmct_rescan_worker);
514 INIT_WORK(&priv->isr_worker, iwmct_irq_read_worker);
516 init_waitqueue_head(&priv->wait_q);
518 sdio_claim_host(func);
519 func->enable_timeout = 10;
521 /* In our HW, setting the block size also wakes up the boot rom. */
522 ret = sdio_set_block_size(func, priv->dbg.block_size);
523 if (ret) {
524 LOG_ERROR(priv, INIT,
525 "sdio_set_block_size() failure: %d\n", ret);
526 goto error_sdio_enable;
529 ret = sdio_enable_func(func);
530 if (ret) {
531 LOG_ERROR(priv, INIT, "sdio_enable_func() failure: %d\n", ret);
532 goto error_sdio_enable;
535 /* init reset and dev_sync states */
536 atomic_set(&priv->reset, 0);
537 atomic_set(&priv->dev_sync, 0);
539 /* init read req queue */
540 INIT_LIST_HEAD(&priv->read_req_list);
542 /* process configurable parameters */
543 iwmct_dbg_init_params(priv);
544 ret = sysfs_create_group(&func->dev.kobj, &iwmct_attribute_group);
545 if (ret) {
546 LOG_ERROR(priv, INIT, "Failed to register attributes and "
547 "initialize module_params\n");
548 goto error_dev_attrs;
551 iwmct_dbgfs_register(priv, DRV_NAME);
553 if (!priv->dbg.direct && priv->dbg.download_trans_blks > 8) {
554 LOG_INFO(priv, INIT,
555 "Reducing transaction to 8 blocks = 2K (from %d)\n",
556 priv->dbg.download_trans_blks);
557 priv->dbg.download_trans_blks = 8;
559 priv->trans_len = priv->dbg.download_trans_blks * priv->dbg.block_size;
560 LOG_INFO(priv, INIT, "Transaction length = %d\n", priv->trans_len);
562 ret = sdio_claim_irq(func, iwmct_irq);
563 if (ret) {
564 LOG_ERROR(priv, INIT, "sdio_claim_irq() failure: %d\n", ret);
565 goto error_claim_irq;
569 /* Enable function's interrupt */
570 sdio_writeb(priv->func, val, addr, &ret);
571 if (ret) {
572 LOG_ERROR(priv, INIT, "Failure writing to "
573 "Interrupt Enable Register (%d): %d\n", addr, ret);
574 goto error_enable_int;
577 sdio_release_host(func);
579 LOG_INFO(priv, INIT, "exit iwmct_probe\n");
581 return ret;
583 error_enable_int:
584 sdio_release_irq(func);
585 error_claim_irq:
586 sdio_disable_func(func);
587 error_dev_attrs:
588 iwmct_dbgfs_unregister(priv->dbgfs);
589 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
590 error_sdio_enable:
591 sdio_release_host(func);
592 return ret;
595 static void iwmct_remove(struct sdio_func *func)
597 struct iwmct_work_struct *read_req;
598 struct iwmct_priv *priv = sdio_get_drvdata(func);
600 LOG_INFO(priv, INIT, "enter\n");
602 sdio_claim_host(func);
603 sdio_release_irq(func);
604 sdio_release_host(func);
606 /* Safely destroy osc workqueue */
607 destroy_workqueue(priv->bus_rescan_wq);
608 destroy_workqueue(priv->wq);
610 sdio_claim_host(func);
611 sdio_disable_func(func);
612 sysfs_remove_group(&func->dev.kobj, &iwmct_attribute_group);
613 iwmct_dbgfs_unregister(priv->dbgfs);
614 sdio_release_host(func);
616 /* free read requests */
617 while (!list_empty(&priv->read_req_list)) {
618 read_req = list_entry(priv->read_req_list.next,
619 struct iwmct_work_struct, list);
621 list_del(&read_req->list);
622 kfree(read_req);
625 kfree(priv);
629 static const struct sdio_device_id iwmct_ids[] = {
630 /* Intel Wireless MultiCom 3200 Top Driver */
631 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1404)},
632 { }, /* Terminating entry */
635 MODULE_DEVICE_TABLE(sdio, iwmct_ids);
637 static struct sdio_driver iwmct_driver = {
638 .probe = iwmct_probe,
639 .remove = iwmct_remove,
640 .name = DRV_NAME,
641 .id_table = iwmct_ids,
644 static int __init iwmct_init(void)
646 int rc;
648 /* Default log filter settings */
649 iwmct_log_set_filter(LOG_SRC_ALL, LOG_SEV_FILTER_RUNTIME);
650 iwmct_log_set_filter(LOG_SRC_FW_MSG, LOG_SEV_FW_FILTER_ALL);
651 iwmct_log_set_fw_filter(LOG_SRC_ALL, FW_LOG_SEV_FILTER_RUNTIME);
653 rc = sdio_register_driver(&iwmct_driver);
655 return rc;
658 static void __exit iwmct_exit(void)
660 sdio_unregister_driver(&iwmct_driver);
663 module_init(iwmct_init);
664 module_exit(iwmct_exit);