2 * ---------------------------------------------------------------------------
6 * Conventional device interface for debugging/monitoring of the
7 * driver and h/w using unicli. This interface is also being used
8 * by the SME linux implementation and the helper apps.
10 * Copyright (C) 2005-2009 by Cambridge Silicon Radio Ltd.
12 * Refer to LICENSE.txt included with this source code for details on
15 * ---------------------------------------------------------------------------
20 * Part of this file contains an example for how to glue the OS layer
21 * with the HIP core lib, the SDIO glue layer, and the SME.
23 * When the unifi_sdio.ko modules loads, the linux kernel calls unifi_load().
24 * unifi_load() calls uf_sdio_load() which is exported by the SDIO glue
25 * layer. uf_sdio_load() registers this driver with the underlying SDIO driver.
26 * When a card is detected, the SDIO glue layer calls register_unifi_sdio()
27 * to pass the SDIO function context and ask the OS layer to initialise
28 * the card. register_unifi_sdio() allocates all the private data of the OS
29 * layer and calls uf_run_unifihelper() to start the SME. The SME calls
30 * unifi_sys_wifi_on_req() which uses the HIP core lib to initialise the card.
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <asm/uaccess.h>
37 #include <linux/jiffies.h>
38 #include <linux/version.h>
40 #include "csr_wifi_hip_unifiversion.h"
41 #include "unifi_priv.h"
42 #include "csr_wifi_hip_conversions.h"
43 #include "unifi_native.h"
45 /* Module parameter variables */
46 int buswidth = 0; /* 0 means use default, values 1,4 */
47 int sdio_clock = 50000; /* kHz */
49 /* fw_init prevents f/w initialisation on error. */
50 int fw_init[MAX_UNIFI_DEVS] = {-1, -1};
52 int led_mask = 0; /* 0x0c00 for dev-pc-1503c, dev-pc-1528a */
53 int disable_hw_reset = 0;
54 int disable_power_control = 0;
55 int enable_wol = UNIFI_WOL_OFF; /* 0 for none, 1 for SDIO IRQ, 2 for PIO */
56 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
57 int tl_80211d = (int)CSR_WIFI_SME_80211D_TRUST_LEVEL_MIB;
59 int sdio_block_size = -1; /* Override SDIO block size */
60 int sdio_byte_mode = 0; /* 0 for block mode + padding, 1 for byte mode */
61 int coredump_max = CSR_WIFI_HIP_NUM_COREDUMP_BUFFERS;
62 int run_bh_once = -1; /* Set for scheduled interrupt mode, -1 = default */
64 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
65 #define UNIFI_LOG_HIP_SIGNALS_FILTER_BULKDATA (1 << 1)
66 #define UNIFI_LOG_HIP_SIGNALS_FILTER_TIMESTAMP (1 << 2)
67 int log_hip_signals = 0;
70 MODULE_DESCRIPTION("CSR UniFi (SDIO)");
72 module_param(buswidth, int, S_IRUGO|S_IWUSR);
73 module_param(sdio_clock, int, S_IRUGO|S_IWUSR);
74 module_param(unifi_debug, int, S_IRUGO|S_IWUSR);
75 module_param_array(fw_init, int, NULL, S_IRUGO|S_IWUSR);
76 module_param(use_5g, int, S_IRUGO|S_IWUSR);
77 module_param(led_mask, int, S_IRUGO|S_IWUSR);
78 module_param(disable_hw_reset, int, S_IRUGO|S_IWUSR);
79 module_param(disable_power_control, int, S_IRUGO|S_IWUSR);
80 module_param(enable_wol, int, S_IRUGO|S_IWUSR);
81 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
82 module_param(tl_80211d, int, S_IRUGO|S_IWUSR);
84 module_param(sdio_block_size, int, S_IRUGO|S_IWUSR);
85 module_param(sdio_byte_mode, int, S_IRUGO|S_IWUSR);
86 module_param(coredump_max, int, S_IRUGO|S_IWUSR);
87 module_param(run_bh_once, int, S_IRUGO|S_IWUSR);
88 module_param(bh_priority, int, S_IRUGO|S_IWUSR);
89 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
90 module_param(log_hip_signals, int, S_IRUGO|S_IWUSR);
93 MODULE_PARM_DESC(buswidth, "SDIO bus width (0=default), set 1 for 1-bit or 4 for 4-bit mode");
94 MODULE_PARM_DESC(sdio_clock, "SDIO bus frequency in kHz, (default = 50 MHz)");
95 MODULE_PARM_DESC(unifi_debug, "Diagnostic reporting level");
96 MODULE_PARM_DESC(fw_init, "Set to 0 to prevent f/w initialization on error");
97 MODULE_PARM_DESC(use_5g, "Use the 5G (802.11a) radio band");
98 MODULE_PARM_DESC(led_mask, "LED mask flags");
99 MODULE_PARM_DESC(disable_hw_reset, "Set to 1 to disable hardware reset");
100 MODULE_PARM_DESC(disable_power_control, "Set to 1 to disable SDIO power control");
101 MODULE_PARM_DESC(enable_wol, "Enable wake-on-wlan function 0=off, 1=SDIO, 2=PIO");
102 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
103 MODULE_PARM_DESC(tl_80211d, "802.11d Trust Level (1-6, default = 5)");
105 MODULE_PARM_DESC(sdio_block_size, "Set to override SDIO block size");
106 MODULE_PARM_DESC(sdio_byte_mode, "Set to 1 for byte mode SDIO");
107 MODULE_PARM_DESC(coredump_max, "Number of chip mini-coredump buffers to allocate");
108 MODULE_PARM_DESC(run_bh_once, "Run BH only when firmware interrupts");
109 MODULE_PARM_DESC(bh_priority, "Modify the BH thread priority");
110 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
111 MODULE_PARM_DESC(log_hip_signals, "Set to 1 to enable HIP signal offline logging");
115 /* Callback for event logging to UDI clients */
116 static void udi_log_event(ul_client_t *client,
117 const u8 *signal, int signal_len,
118 const bulk_data_param_t *bulkdata,
121 static void udi_set_log_filter(ul_client_t *pcli,
122 unifiio_filter_t *udi_filter);
125 /* Mutex to protect access to priv->sme_cli */
126 DEFINE_SEMAPHORE(udi_mutex);
128 s32 CsrHipResultToStatus(CsrResult csrResult)
134 case CSR_RESULT_SUCCESS:
137 case CSR_WIFI_HIP_RESULT_RANGE:
140 case CSR_WIFI_HIP_RESULT_NO_DEVICE:
143 case CSR_WIFI_HIP_RESULT_INVALID_VALUE:
146 case CSR_WIFI_HIP_RESULT_NOT_FOUND:
149 case CSR_WIFI_HIP_RESULT_NO_SPACE:
152 case CSR_WIFI_HIP_RESULT_NO_MEMORY:
155 case CSR_RESULT_FAILURE:
159 /*unifi_warning(card->ospriv, "CsrHipResultToStatus: Unrecognised csrResult error code: %d\n", csrResult);*/
167 trace_putest_cmdid(unifi_putest_command_t putest_cmd)
171 case UNIFI_PUTEST_START:
173 case UNIFI_PUTEST_STOP:
175 case UNIFI_PUTEST_SET_SDIO_CLOCK:
177 case UNIFI_PUTEST_CMD52_READ:
179 case UNIFI_PUTEST_CMD52_BLOCK_READ:
181 case UNIFI_PUTEST_CMD52_WRITE:
183 case UNIFI_PUTEST_DL_FW:
185 case UNIFI_PUTEST_DL_FW_BUFF:
186 return "D/L FW BUFFER";
187 case UNIFI_PUTEST_COREDUMP_PREPARE:
188 return "PREPARE COREDUMP";
189 case UNIFI_PUTEST_GP_READ16:
191 case UNIFI_PUTEST_GP_WRITE16:
194 return "ERROR: unrecognised command";
198 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
199 int uf_register_hip_offline_debug(unifi_priv_t *priv)
201 ul_client_t *udi_cli;
204 udi_cli = ul_register_client(priv, CLI_USING_WIRE_FORMAT, udi_log_event);
205 if (udi_cli == NULL) {
206 /* Too many clients already using this device */
207 unifi_error(priv, "Too many UDI clients already open\n");
210 unifi_trace(priv, UDBG1, "Offline HIP client is registered\n");
212 down(&priv->udi_logging_mutex);
213 udi_cli->event_hook = udi_log_event;
214 unifi_set_udi_hook(priv->card, logging_handler);
215 /* Log all signals by default */
216 for (i = 0; i < SIG_FILTER_SIZE; i++) {
217 udi_cli->signal_filter[i] = 0xFFFF;
219 priv->logging_client = udi_cli;
220 up(&priv->udi_logging_mutex);
225 int uf_unregister_hip_offline_debug(unifi_priv_t *priv)
227 ul_client_t *udi_cli = priv->logging_client;
230 unifi_error(priv, "Unknown HIP client unregister request\n");
234 unifi_trace(priv, UDBG1, "Offline HIP client is unregistered\n");
236 down(&priv->udi_logging_mutex);
237 priv->logging_client = NULL;
238 udi_cli->event_hook = NULL;
239 up(&priv->udi_logging_mutex);
241 ul_deregister_client(udi_cli);
249 * ---------------------------------------------------------------------------
253 * Open and release entry points for the UniFi debug driver.
256 * Normal linux driver args.
260 * ---------------------------------------------------------------------------
263 unifi_open(struct inode *inode, struct file *file)
267 ul_client_t *udi_cli;
271 devno = MINOR(inode->i_rdev) >> 1;
274 * Increase the ref_count for the char device clients.
275 * Make sure you call uf_put_instance() to decreace it if
276 * unifi_open returns an error.
278 priv = uf_get_instance(devno);
280 unifi_error(NULL, "unifi_open: No device present\n");
285 /* Register this instance in the client's list. */
286 /* The minor number determines the nature of the client (Unicli or SME). */
287 if (MINOR(inode->i_rdev) & 0x1) {
288 udi_cli = ul_register_client(priv, CLI_USING_WIRE_FORMAT, udi_log_event);
289 if (udi_cli == NULL) {
290 /* Too many clients already using this device */
291 unifi_error(priv, "Too many clients already open\n");
292 uf_put_instance(devno);
296 unifi_trace(priv, UDBG1, "Client is registered to /dev/unifiudi%d\n", devno);
299 * Even-numbered device nodes are the control application.
300 * This is the userspace helper containing SME or
306 #ifdef CSR_SME_USERSPACE
307 /* Check if a config client is already attached */
310 uf_put_instance(devno);
312 unifi_info(priv, "There is already a configuration client using the character device\n");
316 #endif /* CSR_SME_USERSPACE */
318 #ifdef CSR_SUPPORT_SME
319 udi_cli = ul_register_client(priv,
320 CLI_USING_WIRE_FORMAT | CLI_SME_USERSPACE,
323 /* Config client for native driver */
324 udi_cli = ul_register_client(priv,
326 sme_native_log_event);
328 if (udi_cli == NULL) {
329 /* Too many clients already using this device */
331 uf_put_instance(devno);
333 unifi_error(priv, "Too many clients already open\n");
339 * Fill-in the pointer to the configuration client.
340 * This is the SME userspace helper or unifi_manager.
341 * Not used in the SME embedded version.
343 unifi_trace(priv, UDBG1, "SME client (id:%d s:0x%X) is registered\n",
344 udi_cli->client_id, udi_cli->sender_id);
345 /* Store the SME UniFi Linux Client */
346 if (priv->sme_cli == NULL) {
347 priv->sme_cli = udi_cli;
355 * Store the pointer to the client.
356 * All char driver's entry points will pass this pointer.
358 file->private_data = udi_cli;
366 unifi_release(struct inode *inode, struct file *filp)
368 ul_client_t *udi_cli = (void*)filp->private_data;
374 priv = uf_find_instance(udi_cli->instance);
376 unifi_error(priv, "unifi_close: instance for device not found\n");
380 devno = MINOR(inode->i_rdev) >> 1;
382 /* Even device nodes are the config client (i.e. SME or unifi_manager) */
383 if ((MINOR(inode->i_rdev) & 0x1) == 0) {
385 if (priv->sme_cli != udi_cli) {
386 unifi_notice(priv, "Surprise closing config device: not the sme client\n");
388 unifi_notice(priv, "SME client close (unifi%d)\n", devno);
391 * Clear sme_cli before calling unifi_sys_... so it doesn't try to
392 * queue a reply to the (now gone) SME.
395 priv->sme_cli = NULL;
398 #ifdef CSR_SME_USERSPACE
399 /* Power-down when config client closes */
401 CsrWifiRouterCtrlWifiOffReq req = {{CSR_WIFI_ROUTER_CTRL_HIP_REQ, 0, 0, 0, NULL}};
402 CsrWifiRouterCtrlWifiOffReqHandler(priv, &req.common);
407 /* It is possible that a blocking SME request was made from another process
408 * which did not get read by the SME before the WifiOffReq.
409 * So check for a pending request which will go unanswered and cancel
410 * the wait for event. As only one blocking request can be in progress at
411 * a time, up to one event should be completed.
413 uf_sme_cancel_request(priv, 0);
415 #endif /* CSR_SME_USERSPACE */
418 unifi_trace(priv, UDBG2, "UDI client close (unifiudi%d)\n", devno);
420 /* If the pointer matches the logging client, stop logging. */
421 down(&priv->udi_logging_mutex);
422 if (udi_cli == priv->logging_client) {
423 priv->logging_client = NULL;
425 up(&priv->udi_logging_mutex);
427 if (udi_cli == priv->amp_client) {
428 priv->amp_client = NULL;
432 /* Deregister this instance from the client's list. */
433 ul_deregister_client(udi_cli);
435 uf_put_instance(devno);
438 } /* unifi_release() */
443 * ---------------------------------------------------------------------------
446 * The read() driver entry point.
449 * filp The file descriptor returned by unifi_open()
450 * p The user space buffer to copy the read data
451 * len The size of the p buffer
455 * number of bytes read or an error code on failure
456 * ---------------------------------------------------------------------------
459 unifi_read(struct file *filp, char *p, size_t len, loff_t *poff)
461 ul_client_t *pcli = (void*)filp->private_data;
463 udi_log_t *logptr = NULL;
470 priv = uf_find_instance(pcli->instance);
472 unifi_error(priv, "invalid priv\n");
476 if (!pcli->udi_enabled) {
477 unifi_error(priv, "unifi_read: unknown client.");
481 if (list_empty(&pcli->udi_log)) {
482 if (filp->f_flags & O_NONBLOCK) {
483 /* Non-blocking - just return if the udi_log is empty */
486 /* Blocking - wait on the UDI wait queue */
487 if (wait_event_interruptible(pcli->udi_wq,
488 !list_empty(&pcli->udi_log)))
490 unifi_error(priv, "unifi_read: wait_event_interruptible failed.");
496 /* Read entry from list head and remove it from the list */
497 if (down_interruptible(&pcli->udi_sem)) {
500 l = pcli->udi_log.next;
504 /* Get a pointer to whole struct */
505 logptr = list_entry(l, udi_log_t, q);
506 if (logptr == NULL) {
507 unifi_error(priv, "unifi_read: failed to get event.\n");
511 /* Get the real message */
512 msgptr = &logptr->msg;
513 msglen = msgptr->length;
515 printk(KERN_WARNING "truncated read to %d actual msg len is %lu\n", msglen, (long unsigned int)len);
519 /* and pass it to the client (SME or Unicli). */
520 if (copy_to_user(p, msgptr, msglen))
522 printk(KERN_ERR "Failed to copy UDI log to user\n");
527 /* It is our resposibility to free the message buffer. */
538 * ---------------------------------------------------------------------------
539 * udi_send_signal_unpacked
541 * Sends an unpacked signal to UniFi.
544 * priv Pointer to private context struct
545 * data Pointer to request structure and data to send
546 * data_len Length of data in data pointer.
549 * Number of bytes written, error otherwise.
552 * All clients that use this function to send a signal to the unifi
553 * must use the host formatted structures.
554 * ---------------------------------------------------------------------------
557 udi_send_signal_unpacked(unifi_priv_t *priv, unsigned char* data, uint data_len)
559 CSR_SIGNAL *sigptr = (CSR_SIGNAL*)data;
560 CSR_DATAREF *datarefptr;
561 bulk_data_param_t bulk_data;
563 uint bulk_data_offset = 0;
567 /* Number of bytes in the signal */
568 signal_size = SigGetSize(sigptr);
569 if (!signal_size || (signal_size > data_len)) {
570 unifi_error(priv, "unifi_sme_mlme_req - Invalid signal 0x%x size should be %d bytes\n",
571 sigptr->SignalPrimitiveHeader.SignalId,
575 bytecount = signal_size;
577 /* Get a pointer to the information of the first data reference */
578 datarefptr = (CSR_DATAREF*)&sigptr->u;
580 /* Initialize the offset in the data buffer, bulk data is right after the signal. */
581 bulk_data_offset = signal_size;
583 /* store the references and the size of the bulk data to the bulkdata structure */
584 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++) {
585 /* the length of the bulk data is in the signal */
586 if ((datarefptr+i)->DataLength) {
589 csrResult = unifi_net_data_malloc(priv, &bulk_data.d[i], (datarefptr+i)->DataLength);
590 if (csrResult != CSR_RESULT_SUCCESS) {
591 unifi_error(priv, "udi_send_signal_unpacked: failed to allocate request_data.\n");
595 dest = (void*)bulk_data.d[i].os_data_ptr;
596 memcpy(dest, data + bulk_data_offset, bulk_data.d[i].data_length);
598 bulk_data.d[i].data_length = 0;
601 bytecount += bulk_data.d[i].data_length;
602 /* advance the offset, to point the next bulk data */
603 bulk_data_offset += bulk_data.d[i].data_length;
607 unifi_trace(priv, UDBG3, "SME Send: signal 0x%.4X\n", sigptr->SignalPrimitiveHeader.SignalId);
609 /* Send the signal. */
610 r = ul_send_signal_unpacked(priv, sigptr, &bulk_data);
612 unifi_error(priv, "udi_send_signal_unpacked: send failed (%d)\n", r);
613 for(i=0;i<UNIFI_MAX_DATA_REFERENCES;i++) {
614 if(bulk_data.d[i].data_length != 0) {
615 unifi_net_data_free(priv, &bulk_data.d[i]);
623 } /* udi_send_signal_unpacked() */
628 * ---------------------------------------------------------------------------
629 * udi_send_signal_raw
631 * Sends a packed signal to UniFi.
634 * priv Pointer to private context struct
635 * buf Pointer to request structure and data to send
636 * buflen Length of data in data pointer.
639 * Number of bytes written, error otherwise.
642 * All clients that use this function to send a signal to the unifi
643 * must use the wire formatted structures.
644 * ---------------------------------------------------------------------------
647 udi_send_signal_raw(unifi_priv_t *priv, unsigned char *buf, int buflen)
651 bulk_data_param_t data_ptrs;
653 unsigned int num_data_refs;
660 * The signal is the first thing in buf, the signal id is the
661 * first 16 bits of the signal.
663 /* Number of bytes in the signal */
664 sig_id = GET_SIGNAL_ID(buf);
665 signal_size = buflen;
666 signal_size -= GET_PACKED_DATAREF_LEN(buf, 0);
667 signal_size -= GET_PACKED_DATAREF_LEN(buf, 1);
668 if ((signal_size <= 0) || (signal_size > buflen)) {
669 unifi_error(priv, "udi_send_signal_raw - Couldn't find length of signal 0x%x\n",
674 unifi_trace(priv, UDBG2, "udi_send_signal_raw: signal 0x%.4X len:%d\n",
675 sig_id, signal_size);
676 /* Zero the data ref arrays */
677 memset(&data_ptrs, 0, sizeof(data_ptrs));
680 * Find the number of associated bulk data packets. Scan through
681 * the data refs to check that we have enough data and pick out
682 * pointers to appended bulk data.
685 bytecount = signal_size;
687 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; ++i)
689 unsigned int len = GET_PACKED_DATAREF_LEN(buf, i);
690 unifi_trace(priv, UDBG3, "udi_send_signal_raw: data_ref length = %d\n", len);
695 csrResult = unifi_net_data_malloc(priv, &data_ptrs.d[i], len);
696 if (csrResult != CSR_RESULT_SUCCESS) {
697 unifi_error(priv, "udi_send_signal_raw: failed to allocate request_data.\n");
701 dest = (void*)data_ptrs.d[i].os_data_ptr;
702 memcpy(dest, buf + bytecount, len);
707 data_ptrs.d[i].data_length = len;
710 unifi_trace(priv, UDBG3, "Queueing signal 0x%.4X from UDI with %u data refs\n",
714 if (bytecount > buflen) {
715 unifi_error(priv, "udi_send_signal_raw: Not enough data (%d instead of %d)\n", buflen, bytecount);
720 /* Send the signal calling the function that uses the wire-formatted signals. */
721 r = ul_send_signal_raw(priv, buf, signal_size, &data_ptrs);
723 unifi_error(priv, "udi_send_signal_raw: send failed (%d)\n", r);
728 #ifdef CSR_NATIVE_LINUX
729 if (sig_id == CSR_MLME_POWERMGT_REQUEST_ID) {
730 int power_mode = CSR_GET_UINT16_FROM_LITTLE_ENDIAN((buf +
731 SIZEOF_SIGNAL_HEADER + (UNIFI_MAX_DATA_REFERENCES*SIZEOF_DATAREF)));
732 #ifdef CSR_SUPPORT_WEXT
733 /* Overide the wext power mode to the new value */
734 priv->wext_conf.power_mode = power_mode;
736 /* Configure deep sleep signaling */
737 if (power_mode || (priv->interfacePriv[0]->connected == UnifiNotConnected)) {
738 csrResult = unifi_configure_low_power_mode(priv->card,
739 UNIFI_LOW_POWER_ENABLED,
740 UNIFI_PERIODIC_WAKE_HOST_DISABLED);
742 csrResult = unifi_configure_low_power_mode(priv->card,
743 UNIFI_LOW_POWER_DISABLED,
744 UNIFI_PERIODIC_WAKE_HOST_DISABLED);
749 func_exit_r(bytecount);
752 } /* udi_send_signal_raw */
755 * ---------------------------------------------------------------------------
758 * The write() driver entry point.
759 * A UniFi Debug Interface client such as unicli can write a signal
760 * plus bulk data to the driver for sending to the UniFi chip.
762 * Only one signal may be sent per write operation.
765 * filp The file descriptor returned by unifi_open()
766 * p The user space buffer to get the data from
767 * len The size of the p buffer
771 * number of bytes written or an error code on failure
772 * ---------------------------------------------------------------------------
775 unifi_write(struct file *filp, const char *p, size_t len, loff_t *poff)
777 ul_client_t *pcli = (ul_client_t*)filp->private_data;
780 unsigned char *bufptr;
784 bulk_data_param_t bulkdata;
789 priv = uf_find_instance(pcli->instance);
791 unifi_error(priv, "invalid priv\n");
795 unifi_trace(priv, UDBG5, "unifi_write: len = %d\n", len);
797 if (!pcli->udi_enabled) {
798 unifi_error(priv, "udi disabled\n");
803 * AMP client sends only one signal at a time, so we can use
804 * unifi_net_data_malloc to save the extra copy.
806 if (pcli == priv->amp_client) {
809 unsigned char *signal_buf;
812 csrResult = unifi_net_data_malloc(priv, &bulkdata.d[0], len);
813 if (csrResult != CSR_RESULT_SUCCESS) {
814 unifi_error(priv, "unifi_write: failed to allocate request_data.\n");
819 user_data_buf = (char*)bulkdata.d[0].os_data_ptr;
821 /* Get the data from the AMP client. */
822 if (copy_from_user((void*)user_data_buf, p, len)) {
823 unifi_error(priv, "unifi_write: copy from user failed\n");
824 unifi_net_data_free(priv, &bulkdata.d[0]);
829 bulkdata.d[1].os_data_ptr = NULL;
830 bulkdata.d[1].data_length = 0;
832 /* Number of bytes in the signal */
833 sig_id = GET_SIGNAL_ID(bulkdata.d[0].os_data_ptr);
835 signal_size -= GET_PACKED_DATAREF_LEN(bulkdata.d[0].os_data_ptr, 0);
836 signal_size -= GET_PACKED_DATAREF_LEN(bulkdata.d[0].os_data_ptr, 1);
837 if ((signal_size <= 0) || (signal_size > len)) {
838 unifi_error(priv, "unifi_write - Couldn't find length of signal 0x%x\n",
840 unifi_net_data_free(priv, &bulkdata.d[0]);
845 unifi_trace(priv, UDBG2, "unifi_write: signal 0x%.4X len:%d\n",
846 sig_id, signal_size);
848 /* Allocate a buffer for the signal */
849 signal_buf = kmalloc(signal_size, GFP_KERNEL);
851 unifi_net_data_free(priv, &bulkdata.d[0]);
856 /* Get the signal from the os_data_ptr */
857 memcpy(signal_buf, bulkdata.d[0].os_data_ptr, signal_size);
858 signal_buf[5] = (pcli->sender_id >> 8) & 0xff;
860 if (signal_size < len) {
861 /* Remove the signal from the os_data_ptr */
862 bulkdata.d[0].data_length -= signal_size;
863 bulkdata.d[0].os_data_ptr += signal_size;
865 bulkdata.d[0].data_length = 0;
866 bulkdata.d[0].os_data_ptr = NULL;
869 /* Send the signal calling the function that uses the wire-formatted signals. */
870 r = ul_send_signal_raw(priv, signal_buf, signal_size, &bulkdata);
872 unifi_error(priv, "unifi_write: send failed (%d)\n", r);
873 if (bulkdata.d[0].os_data_ptr != NULL) {
874 unifi_net_data_free(priv, &bulkdata.d[0]);
878 /* Free the signal buffer and return */
883 buf = kmalloc(len, GFP_KERNEL);
888 /* Get the data from the client (SME or Unicli). */
889 if (copy_from_user((void*)buf, p, len)) {
890 unifi_error(priv, "copy from user failed\n");
896 * In SME userspace build read() contains a SYS or MGT message.
897 * Note that even though the SME sends one signal at a time, we can not
898 * use unifi_net_data_malloc because in the early stages, before having
899 * initialised the core, it will fail since the I/O block size is unknown.
901 #ifdef CSR_SME_USERSPACE
902 if (pcli->configuration & CLI_SME_USERSPACE) {
903 CsrWifiRouterTransportRecv(priv, buf, len);
909 /* ul_send_signal_raw will do a sanity check of len against signal content */
912 * udi_send_signal_raw() and udi_send_signal_unpacked() return the number of bytes consumed.
913 * A write call can pass multiple signal concatenated together.
918 while (remaining > 0)
923 * Set the SenderProcessId.
924 * The SignalPrimitiveHeader is the first 3 16-bit words of the signal,
925 * the SenderProcessId is bytes 4,5.
926 * The MSB of the sender ID needs to be set to the client ID.
927 * The LSB is controlled by the SME.
929 bufptr[5] = (pcli->sender_id >> 8) & 0xff;
931 /* use the appropriate interface, depending on the clients' configuration */
932 if (pcli->configuration & CLI_USING_WIRE_FORMAT) {
933 unifi_trace(priv, UDBG1, "unifi_write: call udi_send_signal().\n");
934 r = udi_send_signal_raw(priv, bufptr, remaining);
936 r = udi_send_signal_unpacked(priv, bufptr, remaining);
939 /* Set the return value to the error code */
940 unifi_error(priv, "unifi_write: (udi or sme)_send_signal() returns %d\n", r);
951 func_exit_r(bytes_written);
953 return bytes_written;
954 } /* unifi_write() */
957 static const char* build_type_to_string(unsigned char build_type)
961 case UNIFI_BUILD_NME: return "NME";
962 case UNIFI_BUILD_WEXT: return "WEXT";
963 case UNIFI_BUILD_AP: return "AP";
970 * ----------------------------------------------------------------
973 * Ioctl handler for unifi driver.
976 * inodep Pointer to inode structure.
977 * filp Pointer to file structure.
978 * cmd Ioctl cmd passed by user.
979 * arg Ioctl arg passed by user.
982 * 0 on success, -ve error code on error.
983 * ----------------------------------------------------------------
986 unifi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
988 ul_client_t *pcli = (ul_client_t*)filp->private_data;
990 struct net_device *dev;
995 #if (defined CSR_SUPPORT_SME)
996 unifi_cfg_command_t cfg_cmd;
997 #if (defined CSR_SUPPORT_WEXT)
998 CsrWifiSmeCoexConfig coex_config;
999 unsigned char uchar_param;
1000 unsigned char varbind[MAX_VARBIND_LENGTH];
1004 unifi_putest_command_t putest_cmd;
1006 priv = uf_find_instance(pcli->instance);
1008 unifi_error(priv, "ioctl error: unknown instance=%d\n", pcli->instance);
1012 unifi_trace(priv, UDBG5, "unifi_ioctl: cmd=0x%X, arg=0x%lX\n", cmd, arg);
1016 case UNIFI_GET_UDI_ENABLE:
1017 unifi_trace(priv, UDBG4, "UniFi Get UDI Enable\n");
1019 down(&priv->udi_logging_mutex);
1020 int_param = (priv->logging_client == NULL) ? 0 : 1;
1021 up(&priv->udi_logging_mutex);
1023 if (put_user(int_param, (int*)arg))
1025 unifi_error(priv, "UNIFI_GET_UDI_ENABLE: Failed to copy to user\n");
1031 case UNIFI_SET_UDI_ENABLE:
1032 unifi_trace(priv, UDBG4, "UniFi Set UDI Enable\n");
1033 if (get_user(int_param, (int*)arg))
1035 unifi_error(priv, "UNIFI_SET_UDI_ENABLE: Failed to copy from user\n");
1040 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
1041 if (log_hip_signals) {
1042 unifi_error(priv, "omnicli cannot be used when log_hip_signals is used\n");
1048 down(&priv->udi_logging_mutex);
1050 pcli->event_hook = udi_log_event;
1051 unifi_set_udi_hook(priv->card, logging_handler);
1052 /* Log all signals by default */
1053 for (i = 0; i < SIG_FILTER_SIZE; i++) {
1054 pcli->signal_filter[i] = 0xFFFF;
1056 priv->logging_client = pcli;
1059 priv->logging_client = NULL;
1060 pcli->event_hook = NULL;
1062 up(&priv->udi_logging_mutex);
1067 unifi_trace(priv, UDBG4, "UniFi Set MIB\n");
1068 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
1069 /* Read first 2 bytes and check length */
1070 if (copy_from_user((void*)varbind, (void*)arg, 2)) {
1072 "UNIFI_SET_MIB: Failed to copy in varbind header\n");
1077 if ((vblen + 2) > MAX_VARBIND_LENGTH) {
1079 "UNIFI_SET_MIB: Varbind too long (%d, limit %d)\n",
1080 (vblen+2), MAX_VARBIND_LENGTH);
1084 /* Read rest of varbind */
1085 if (copy_from_user((void*)(varbind+2), (void*)(arg+2), vblen)) {
1086 unifi_error(priv, "UNIFI_SET_MIB: Failed to copy in varbind\n");
1093 r = sme_mgt_mib_set(priv, varbind, vblen);
1098 unifi_notice(priv, "UNIFI_SET_MIB: Unsupported.\n");
1099 #endif /* CSR_SUPPORT_WEXT */
1103 unifi_trace(priv, UDBG4, "UniFi Get MIB\n");
1104 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
1105 /* Read first 2 bytes and check length */
1106 if (copy_from_user((void*)varbind, (void*)arg, 2)) {
1107 unifi_error(priv, "UNIFI_GET_MIB: Failed to copy in varbind header\n");
1112 if ((vblen+2) > MAX_VARBIND_LENGTH) {
1113 unifi_error(priv, "UNIFI_GET_MIB: Varbind too long (%d, limit %d)\n",
1114 (vblen+2), MAX_VARBIND_LENGTH);
1118 /* Read rest of varbind */
1119 if (copy_from_user((void*)(varbind+2), (void*)(arg+2), vblen)) {
1120 unifi_error(priv, "UNIFI_GET_MIB: Failed to copy in varbind\n");
1126 r = sme_mgt_mib_get(priv, varbind, &vblen);
1130 /* copy out varbind */
1131 if (vblen > MAX_VARBIND_LENGTH) {
1133 "UNIFI_GET_MIB: Varbind result too long (%d, limit %d)\n",
1134 vblen, MAX_VARBIND_LENGTH);
1138 if (copy_to_user((void*)arg, varbind, vblen)) {
1143 unifi_notice(priv, "UNIFI_GET_MIB: Unsupported.\n");
1144 #endif /* CSR_SUPPORT_WEXT */
1148 #if (defined CSR_SUPPORT_SME)
1149 if (get_user(cfg_cmd, (unifi_cfg_command_t*)arg))
1151 unifi_error(priv, "UNIFI_CFG: Failed to get the command\n");
1156 unifi_trace(priv, UDBG1, "UNIFI_CFG: Command is %d (t=%u) sz=%d\n",
1157 cfg_cmd, jiffies_to_msecs(jiffies), sizeof(unifi_cfg_command_t));
1159 case UNIFI_CFG_POWER:
1160 r = unifi_cfg_power(priv, (unsigned char*)arg);
1162 case UNIFI_CFG_POWERSAVE:
1163 r = unifi_cfg_power_save(priv, (unsigned char*)arg);
1165 case UNIFI_CFG_POWERSUPPLY:
1166 r = unifi_cfg_power_supply(priv, (unsigned char*)arg);
1168 case UNIFI_CFG_FILTER:
1169 r = unifi_cfg_packet_filters(priv, (unsigned char*)arg);
1172 r = unifi_cfg_get_info(priv, (unsigned char*)arg);
1174 case UNIFI_CFG_WMM_QOSINFO:
1175 r = unifi_cfg_wmm_qos_info(priv, (unsigned char*)arg);
1177 case UNIFI_CFG_WMM_ADDTS:
1178 r = unifi_cfg_wmm_addts(priv, (unsigned char*)arg);
1180 case UNIFI_CFG_WMM_DELTS:
1181 r = unifi_cfg_wmm_delts(priv, (unsigned char*)arg);
1183 case UNIFI_CFG_STRICT_DRAFT_N:
1184 r = unifi_cfg_strict_draft_n(priv, (unsigned char*)arg);
1186 case UNIFI_CFG_ENABLE_OKC:
1187 r = unifi_cfg_enable_okc(priv, (unsigned char*)arg);
1189 #ifdef CSR_SUPPORT_SME
1190 case UNIFI_CFG_CORE_DUMP:
1191 CsrWifiRouterCtrlWifiOffIndSend(priv->CSR_WIFI_SME_IFACEQUEUE,0,CSR_WIFI_SME_CONTROL_INDICATION_ERROR);
1192 unifi_trace(priv, UDBG2, "UNIFI_CFG_CORE_DUMP: sent wifi off indication\n");
1195 #ifdef CSR_SUPPORT_WEXT_AP
1196 case UNIFI_CFG_SET_AP_CONFIG:
1197 r= unifi_cfg_set_ap_config(priv,(unsigned char*)arg);
1201 unifi_error(priv, "UNIFI_CFG: Unknown Command (%d)\n", cfg_cmd);
1210 if (get_user(putest_cmd, (unifi_putest_command_t*)arg))
1212 unifi_error(priv, "UNIFI_PUTEST: Failed to get the command\n");
1217 unifi_trace(priv, UDBG1, "UNIFI_PUTEST: Command is %s\n",
1218 trace_putest_cmdid(putest_cmd));
1219 switch (putest_cmd) {
1220 case UNIFI_PUTEST_START:
1221 r = unifi_putest_start(priv, (unsigned char*)arg);
1223 case UNIFI_PUTEST_STOP:
1224 r = unifi_putest_stop(priv, (unsigned char*)arg);
1226 case UNIFI_PUTEST_SET_SDIO_CLOCK:
1227 r = unifi_putest_set_sdio_clock(priv, (unsigned char*)arg);
1229 case UNIFI_PUTEST_CMD52_READ:
1230 r = unifi_putest_cmd52_read(priv, (unsigned char*)arg);
1232 case UNIFI_PUTEST_CMD52_BLOCK_READ:
1233 r = unifi_putest_cmd52_block_read(priv, (unsigned char*)arg);
1235 case UNIFI_PUTEST_CMD52_WRITE:
1236 r = unifi_putest_cmd52_write(priv, (unsigned char*)arg);
1238 case UNIFI_PUTEST_DL_FW:
1239 r = unifi_putest_dl_fw(priv, (unsigned char*)arg);
1241 case UNIFI_PUTEST_DL_FW_BUFF:
1242 r = unifi_putest_dl_fw_buff(priv, (unsigned char*)arg);
1244 case UNIFI_PUTEST_COREDUMP_PREPARE:
1245 r = unifi_putest_coredump_prepare(priv, (unsigned char*)arg);
1247 case UNIFI_PUTEST_GP_READ16:
1248 r = unifi_putest_gp_read16(priv, (unsigned char*)arg);
1250 case UNIFI_PUTEST_GP_WRITE16:
1251 r = unifi_putest_gp_write16(priv, (unsigned char*)arg);
1254 unifi_error(priv, "UNIFI_PUTEST: Unknown Command (%d)\n", putest_cmd);
1260 case UNIFI_BUILD_TYPE:
1261 unifi_trace(priv, UDBG2, "UNIFI_BUILD_TYPE userspace=%s\n", build_type_to_string(*(unsigned char*)arg));
1262 #ifndef CSR_SUPPORT_WEXT_AP
1263 if (UNIFI_BUILD_AP == *(unsigned char*)arg)
1265 unifi_error(priv, "Userspace has AP support, which is incompatible\n");
1269 #ifndef CSR_SUPPORT_WEXT
1270 if (UNIFI_BUILD_WEXT == *(unsigned char*)arg)
1272 unifi_error(priv, "Userspace has WEXT support, which is incompatible\n");
1277 unifi_trace(priv, UDBG2, "UNIFI_INIT_HW.\n");
1278 priv->init_progress = UNIFI_INIT_NONE;
1280 #if defined(CSR_SUPPORT_WEXT) || defined (CSR_NATIVE_LINUX)
1281 /* At this point we are ready to start the SME. */
1282 r = sme_mgt_wifi_on(priv);
1290 case UNIFI_INIT_NETDEV:
1292 /* get the proper interfaceTagId */
1294 netInterface_priv_t *interfacePriv = priv->interfacePriv[interfaceTag];
1296 dev = priv->netdev[interfaceTag];
1297 unifi_trace(priv, UDBG2, "UNIFI_INIT_NETDEV.\n");
1299 if (copy_from_user((void*)dev->dev_addr, (void*)arg, 6)) {
1304 /* Attach the network device to the stack */
1305 if (!interfacePriv->netdev_registered)
1307 r = uf_register_netdev(priv,interfaceTag);
1309 unifi_error(priv, "Failed to register the network device.\n");
1314 /* Apply scheduled interrupt mode, if requested by module param */
1315 if (run_bh_once != -1) {
1316 unifi_set_interrupt_mode(priv->card, (u32)run_bh_once);
1319 priv->init_progress = UNIFI_INIT_COMPLETED;
1321 /* Firmware initialisation is complete, so let the SDIO bus
1322 * clock be raised when convienent to the core.
1324 unifi_request_max_sdio_clock(priv->card);
1326 #ifdef CSR_SUPPORT_WEXT
1327 /* Notify the Android wpa_supplicant that we are ready */
1328 wext_send_started_event(priv);
1331 unifi_info(priv, "UniFi ready\n");
1333 #ifdef ANDROID_BUILD
1334 /* Release the wakelock */
1335 unifi_trace(priv, UDBG1, "netdev_init: release wake lock\n");
1336 wake_unlock(&unifi_sdio_wake_lock);
1338 #ifdef CSR_NATIVE_SOFTMAC /* For softmac dev, force-enable the network interface rather than wait for a connected-ind */
1340 struct net_device *dev = priv->netdev[interfaceTag];
1341 #ifdef CSR_SUPPORT_WEXT
1342 interfacePriv->wait_netdev_change = TRUE;
1344 netif_carrier_on(dev);
1349 case UNIFI_GET_INIT_STATUS:
1350 unifi_trace(priv, UDBG2, "UNIFI_GET_INIT_STATUS.\n");
1351 if (put_user(priv->init_progress, (int*)arg))
1353 printk(KERN_ERR "UNIFI_GET_INIT_STATUS: Failed to copy to user\n");
1360 unifi_trace(priv, UDBG4, "Kick UniFi\n");
1361 unifi_sdio_interrupt_handler(priv->card);
1364 case UNIFI_SET_DEBUG:
1366 unifi_trace(priv, UDBG4, "unifi_debug set to %d\n", unifi_debug);
1369 case UNIFI_SET_TRACE:
1370 /* no longer supported */
1375 case UNIFI_SET_UDI_LOG_MASK:
1377 unifiio_filter_t udi_filter;
1378 uint16_t *sig_ids_addr;
1379 #define UF_MAX_SIG_IDS 128 /* Impose a sensible limit */
1381 if (copy_from_user((void*)(&udi_filter), (void*)arg, sizeof(udi_filter))) {
1385 if ((udi_filter.action < UfSigFil_AllOn) ||
1386 (udi_filter.action > UfSigFil_SelectOff))
1389 "UNIFI_SET_UDI_LOG_MASK: Bad action value: %d\n",
1394 /* No signal list for "All" actions */
1395 if ((udi_filter.action == UfSigFil_AllOn) ||
1396 (udi_filter.action == UfSigFil_AllOff))
1398 udi_filter.num_sig_ids = 0;
1401 if (udi_filter.num_sig_ids > UF_MAX_SIG_IDS) {
1403 "UNIFI_SET_UDI_LOG_MASK: too many signal ids (%d, max %d)\n",
1404 udi_filter.num_sig_ids, UF_MAX_SIG_IDS);
1409 /* Copy in signal id list if given */
1410 if (udi_filter.num_sig_ids > 0) {
1411 /* Preserve userspace address of sig_ids array */
1412 sig_ids_addr = udi_filter.sig_ids;
1413 /* Allocate kernel memory for sig_ids and copy to it */
1414 udi_filter.sig_ids =
1415 kmalloc(udi_filter.num_sig_ids * sizeof(uint16_t), GFP_KERNEL);
1416 if (!udi_filter.sig_ids) {
1420 if (copy_from_user((void*)udi_filter.sig_ids,
1421 (void*)sig_ids_addr,
1422 udi_filter.num_sig_ids * sizeof(uint16_t)))
1424 kfree(udi_filter.sig_ids);
1430 udi_set_log_filter(pcli, &udi_filter);
1432 if (udi_filter.num_sig_ids > 0) {
1433 kfree(udi_filter.sig_ids);
1438 case UNIFI_SET_AMP_ENABLE:
1439 unifi_trace(priv, UDBG4, "UniFi Set AMP Enable\n");
1440 if (get_user(int_param, (int*)arg))
1442 unifi_error(priv, "UNIFI_SET_AMP_ENABLE: Failed to copy from user\n");
1448 priv->amp_client = pcli;
1450 priv->amp_client = NULL;
1454 buf = (u8*)&int_param;
1455 buf[0] = UNIFI_SOFT_COMMAND_Q_LENGTH - 1;
1456 buf[1] = UNIFI_SOFT_TRAFFIC_Q_LENGTH - 1;
1457 if (copy_to_user((void*)arg, &int_param, sizeof(int))) {
1463 case UNIFI_SET_UDI_SNAP_MASK:
1465 unifiio_snap_filter_t snap_filter;
1467 if (copy_from_user((void*)(&snap_filter), (void*)arg, sizeof(snap_filter))) {
1472 if (pcli->snap_filter.count) {
1473 pcli->snap_filter.count = 0;
1474 kfree(pcli->snap_filter.protocols);
1477 if (snap_filter.count == 0) {
1481 pcli->snap_filter.protocols = kmalloc(snap_filter.count * sizeof(u16), GFP_KERNEL);
1482 if (!pcli->snap_filter.protocols) {
1486 if (copy_from_user((void*)pcli->snap_filter.protocols,
1487 (void*)snap_filter.protocols,
1488 snap_filter.count * sizeof(u16)))
1490 kfree(pcli->snap_filter.protocols);
1495 pcli->snap_filter.count = snap_filter.count;
1500 case UNIFI_SME_PRESENT:
1503 unifi_trace(priv, UDBG4, "UniFi SME Present IOCTL.\n");
1504 if (copy_from_user((void*)(&int_param), (void*)arg, sizeof(int)))
1506 printk(KERN_ERR "UNIFI_SME_PRESENT: Failed to copy from user\n");
1511 priv->sme_is_present = int_param;
1512 if (priv->sme_is_present == 1) {
1513 ind = CONFIG_SME_PRESENT;
1515 ind = CONFIG_SME_NOT_PRESENT;
1517 /* Send an indication to the helper app. */
1518 ul_log_config_ind(priv, &ind, sizeof(u8));
1522 case UNIFI_CFG_PERIOD_TRAFFIC:
1524 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
1525 CsrWifiSmeCoexConfig coexConfig;
1526 #endif /* CSR_SUPPORT_SME && CSR_SUPPORT_WEXT */
1527 unifi_trace(priv, UDBG4, "UniFi Configure Periodic Traffic.\n");
1528 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
1529 if (copy_from_user((void*)(&uchar_param), (void*)arg, sizeof(unsigned char))) {
1530 unifi_error(priv, "UNIFI_CFG_PERIOD_TRAFFIC: Failed to copy from user\n");
1535 if (uchar_param == 0) {
1536 r = sme_mgt_coex_config_get(priv, &coexConfig);
1538 unifi_error(priv, "UNIFI_CFG_PERIOD_TRAFFIC: Get unifi_CoexInfoValue failed.\n");
1541 if (copy_to_user((void*)(arg + 1),
1543 sizeof(CsrWifiSmeCoexConfig))) {
1550 if (copy_from_user((void*)(&coex_config), (void*)(arg + 1), sizeof(CsrWifiSmeCoexConfig)))
1552 unifi_error(priv, "UNIFI_CFG_PERIOD_TRAFFIC: Failed to copy from user\n");
1557 coexConfig = coex_config;
1558 r = sme_mgt_coex_config_set(priv, &coexConfig);
1560 unifi_error(priv, "UNIFI_CFG_PERIOD_TRAFFIC: Set unifi_CoexInfoValue failed.\n");
1564 #endif /* CSR_SUPPORT_SME && CSR_SUPPORT_WEXT */
1567 case UNIFI_CFG_UAPSD_TRAFFIC:
1568 unifi_trace(priv, UDBG4, "UniFi Configure U-APSD Mask.\n");
1569 #if (defined CSR_SUPPORT_SME) && (defined CSR_SUPPORT_WEXT)
1570 if (copy_from_user((void*)(&uchar_param), (void*)arg, sizeof(unsigned char))) {
1571 unifi_error(priv, "UNIFI_CFG_UAPSD_TRAFFIC: Failed to copy from user\n");
1575 unifi_trace(priv, UDBG4, "New U-APSD Mask: 0x%x\n", uchar_param);
1576 #endif /* CSR_SUPPORT_SME && CSR_SUPPORT_WEXT */
1579 #ifndef UNIFI_DISABLE_COREDUMP
1580 case UNIFI_COREDUMP_GET_REG:
1581 unifi_trace(priv, UDBG4, "Mini-coredump data request\n");
1583 unifiio_coredump_req_t dump_req; /* Public OS layer structure */
1584 unifi_coredump_req_t priv_req; /* Private HIP structure */
1586 if (copy_from_user((void*)(&dump_req), (void*)arg, sizeof(dump_req))) {
1590 memset(&priv_req, 0, sizeof(priv_req));
1591 priv_req.index = dump_req.index;
1592 priv_req.offset = dump_req.offset;
1594 /* Convert OS-layer's XAP memory space ID to HIP's ID in case they differ */
1595 switch (dump_req.space) {
1596 case UNIFIIO_COREDUMP_MAC_REG: priv_req.space = UNIFI_COREDUMP_MAC_REG; break;
1597 case UNIFIIO_COREDUMP_PHY_REG: priv_req.space = UNIFI_COREDUMP_PHY_REG; break;
1598 case UNIFIIO_COREDUMP_SH_DMEM: priv_req.space = UNIFI_COREDUMP_SH_DMEM; break;
1599 case UNIFIIO_COREDUMP_MAC_DMEM: priv_req.space = UNIFI_COREDUMP_MAC_DMEM; break;
1600 case UNIFIIO_COREDUMP_PHY_DMEM: priv_req.space = UNIFI_COREDUMP_PHY_DMEM; break;
1601 case UNIFIIO_COREDUMP_TRIGGER_MAGIC: priv_req.space = UNIFI_COREDUMP_TRIGGER_MAGIC; break;
1607 if (priv_req.space == UNIFI_COREDUMP_TRIGGER_MAGIC) {
1608 /* Force a coredump grab now */
1609 unifi_trace(priv, UDBG2, "UNIFI_COREDUMP_GET_REG: Force capture\n");
1610 csrResult = unifi_coredump_capture(priv->card, &priv_req);
1611 r = CsrHipResultToStatus(csrResult);
1612 unifi_trace(priv, UDBG5, "UNIFI_COREDUMP_GET_REG: status %d\n", r);
1614 /* Retrieve the appropriate register entry */
1615 csrResult = unifi_coredump_get_value(priv->card, &priv_req);
1616 r = CsrHipResultToStatus(csrResult);
1618 unifi_trace(priv, UDBG5, "UNIFI_COREDUMP_GET_REG: Status %d\n", r);
1621 /* Update the OS-layer structure with values returned in the private */
1622 dump_req.value = priv_req.value;
1623 dump_req.timestamp = priv_req.timestamp;
1624 dump_req.requestor = priv_req.requestor;
1625 dump_req.serial = priv_req.serial;
1626 dump_req.chip_ver = priv_req.chip_ver;
1627 dump_req.fw_ver = priv_req.fw_ver;
1628 dump_req.drv_build = 0;
1630 unifi_trace(priv, UDBG6,
1631 "Dump: %d (seq %d): V:0x%04x (%d) @0x%02x:%04x = 0x%04x\n",
1632 dump_req.index, dump_req.serial,
1633 dump_req.chip_ver, dump_req.drv_build,
1634 dump_req.space, dump_req.offset, dump_req.value);
1636 if (copy_to_user((void*)arg, (void*)&dump_req, sizeof(dump_req))) {
1649 } /* unifi_ioctl() */
1654 unifi_poll(struct file *filp, poll_table *wait)
1656 ul_client_t *pcli = (ul_client_t*)filp->private_data;
1657 unsigned int mask = 0;
1662 ready = !list_empty(&pcli->udi_log);
1664 poll_wait(filp, &pcli->udi_wq, wait);
1667 mask |= POLLIN | POLLRDNORM; /* readable */
1673 } /* unifi_poll() */
1678 * ---------------------------------------------------------------------------
1679 * udi_set_log_filter
1681 * Configure the bit mask that determines which signal primitives are
1682 * passed to the logging process.
1685 * pcli Pointer to the client to configure.
1686 * udi_filter Pointer to a unifiio_filter_t containing instructions.
1692 * SigGetFilterPos() returns a 32-bit value that contains an index and a
1693 * mask for accessing a signal_filter array. The top 16 bits specify an
1694 * index into a signal_filter, the bottom 16 bits specify a mask to
1696 * ---------------------------------------------------------------------------
1699 udi_set_log_filter(ul_client_t *pcli, unifiio_filter_t *udi_filter)
1704 if (udi_filter->action == UfSigFil_AllOn)
1706 for (i = 0; i < SIG_FILTER_SIZE; i++) {
1707 pcli->signal_filter[i] = 0xFFFF;
1710 else if (udi_filter->action == UfSigFil_AllOff)
1712 for (i = 0; i < SIG_FILTER_SIZE; i++) {
1713 pcli->signal_filter[i] = 0;
1716 else if (udi_filter->action == UfSigFil_SelectOn)
1718 for (i = 0; i < udi_filter->num_sig_ids; i++) {
1719 filter_pos = SigGetFilterPos(udi_filter->sig_ids[i]);
1720 if (filter_pos == 0xFFFFFFFF)
1723 "Unrecognised signal id (0x%X) specifed in logging filter\n",
1724 udi_filter->sig_ids[i]);
1726 pcli->signal_filter[filter_pos >> 16] |= (filter_pos & 0xFFFF);
1730 else if (udi_filter->action == UfSigFil_SelectOff)
1732 for (i = 0; i < udi_filter->num_sig_ids; i++) {
1733 filter_pos = SigGetFilterPos(udi_filter->sig_ids[i]);
1734 if (filter_pos == 0xFFFFFFFF)
1737 "Unrecognised signal id (0x%X) specifed in logging filter\n",
1738 udi_filter->sig_ids[i]);
1740 pcli->signal_filter[filter_pos >> 16] &= ~(filter_pos & 0xFFFF);
1745 } /* udi_set_log_filter() */
1749 * ---------------------------------------------------------------------------
1752 * Callback function to be registered as the UDI hook callback.
1753 * Copies the signal content into a new udi_log_t struct and adds
1754 * it to the read queue for this UDI client.
1757 * pcli A pointer to the client instance.
1758 * signal Pointer to the received signal.
1759 * signal_len Size of the signal structure in bytes.
1760 * bulkdata Pointers to any associated bulk data.
1761 * dir Direction of the signal. Zero means from host,
1762 * non-zero means to host.
1766 * ---------------------------------------------------------------------------
1769 udi_log_event(ul_client_t *pcli,
1770 const u8 *signal, int signal_len,
1771 const bulk_data_param_t *bulkdata,
1780 #ifdef OMNICLI_LINUX_EXTRA_LOG
1781 static volatile unsigned int printk_cpu = UINT_MAX;
1782 unsigned long long t;
1783 unsigned long nanosec_rem;
1784 unsigned long n_1000;
1789 /* Just a sanity check */
1790 if ((signal == NULL) || (signal_len <= 0)) {
1794 #ifdef CSR_WIFI_HIP_DEBUG_OFFLINE
1795 /* When HIP offline signal logging is enabled, omnicli cannot run */
1796 if (log_hip_signals)
1799 if (log_hip_signals & UNIFI_LOG_HIP_SIGNALS_FILTER_TIMESTAMP)
1801 int timestamp = jiffies_to_msecs(jiffies);
1802 unifi_debug_log_to_buf("T:");
1803 unifi_debug_log_to_buf("%04X%04X ", *(((u16*)×tamp) + 1),
1808 unifi_debug_log_to_buf("S%s:%04X R:%04X D:%04X ",
1811 *(u16*)(signal + 2),
1812 *(u16*)(signal + 4));
1813 unifi_debug_hex_to_buf(signal + 6, signal_len - 6);
1815 /* Add bulk data (assume 1 bulk data per signal) */
1816 if ((log_hip_signals & UNIFI_LOG_HIP_SIGNALS_FILTER_BULKDATA) &&
1817 (bulkdata->d[0].data_length > 0))
1819 unifi_debug_log_to_buf("\nD:");
1820 unifi_debug_hex_to_buf(bulkdata->d[0].os_data_ptr, bulkdata->d[0].data_length);
1822 unifi_debug_log_to_buf("\n");
1828 #ifdef CSR_NATIVE_LINUX
1829 uf_native_process_udi_signal(pcli, signal, signal_len, bulkdata, dir);
1833 * Apply the logging filter - only report signals that have their
1834 * bit set in the filter mask.
1836 filter_pos = SigGetFilterPos(GET_SIGNAL_ID(signal));
1838 if ((filter_pos != 0xFFFFFFFF) &&
1839 ((pcli->signal_filter[filter_pos >> 16] & (filter_pos & 0xFFFF)) == 0))
1841 /* Signal is not wanted by client */
1846 /* Calculate the buffer we need to store signal plus bulk data */
1847 total_len = signal_len;
1848 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++) {
1849 total_len += bulkdata->d[i].data_length;
1852 /* Allocate log structure plus actual signal. */
1853 logptr = (udi_log_t *)kmalloc(sizeof(udi_log_t) + total_len, GFP_KERNEL);
1855 if (logptr == NULL) {
1857 "Failed to allocate %lu bytes for a UDI log record\n",
1858 (long unsigned int)(sizeof(udi_log_t) + total_len));
1862 /* Fill in udi_log struct */
1863 INIT_LIST_HEAD(&logptr->q);
1864 msgptr = &logptr->msg;
1865 msgptr->length = sizeof(udi_msg_t) + total_len;
1866 #ifdef OMNICLI_LINUX_EXTRA_LOG
1867 t = cpu_clock(printk_cpu);
1868 nanosec_rem = do_div(t, 1000000000);
1869 n_1000 = nanosec_rem/1000;
1870 msgptr->timestamp = (t <<10 ) | ((unsigned long)(n_1000 >> 10) & 0x3ff);
1872 msgptr->timestamp = jiffies_to_msecs(jiffies);
1874 msgptr->direction = dir;
1875 msgptr->signal_length = signal_len;
1877 /* Copy signal and bulk data to the log */
1878 p = (u8 *)(msgptr + 1);
1879 memcpy(p, signal, signal_len);
1882 /* Append any bulk data */
1883 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++) {
1884 int len = bulkdata->d[i].data_length;
1887 * Len here might not be the same as the length in the bulk data slot.
1888 * The slot length will always be even, but len could be odd.
1891 if (bulkdata->d[i].os_data_ptr) {
1892 memcpy(p, bulkdata->d[i].os_data_ptr, len);
1900 /* Add to tail of log queue */
1901 if (down_interruptible(&pcli->udi_sem)) {
1902 printk(KERN_WARNING "udi_log_event_q: Failed to get udi sem\n");
1907 list_add_tail(&logptr->q, &pcli->udi_log);
1910 /* Wake any waiting user process */
1911 wake_up_interruptible(&pcli->udi_wq);
1914 } /* udi_log_event() */
1916 #ifdef CSR_SME_USERSPACE
1918 uf_sme_queue_message(unifi_priv_t *priv, u8 *buffer, int length)
1926 /* Just a sanity check */
1927 if ((buffer == NULL) || (length <= 0)) {
1931 /* Allocate log structure plus actual signal. */
1932 logptr = (udi_log_t *)kmalloc(sizeof(udi_log_t) + length, GFP_ATOMIC);
1933 if (logptr == NULL) {
1934 unifi_error(priv, "Failed to allocate %d bytes for an SME message\n",
1935 sizeof(udi_log_t) + length);
1940 /* Fill in udi_log struct */
1941 INIT_LIST_HEAD(&logptr->q);
1942 msgptr = &logptr->msg;
1943 msgptr->length = sizeof(udi_msg_t) + length;
1944 msgptr->signal_length = length;
1946 /* Copy signal and bulk data to the log */
1947 p = (u8 *)(msgptr + 1);
1948 memcpy(p, buffer, length);
1950 /* Add to tail of log queue */
1952 if (priv->sme_cli == NULL) {
1956 unifi_info(priv, "Message for the SME dropped, SME has gone away\n");
1960 down(&priv->sme_cli->udi_sem);
1961 list_add_tail(&logptr->q, &priv->sme_cli->udi_log);
1962 up(&priv->sme_cli->udi_sem);
1964 /* Wake any waiting user process */
1965 wake_up_interruptible(&priv->sme_cli->udi_wq);
1968 /* It is our responsibility to free the buffer allocated in build_packed_*() */
1975 } /* uf_sme_queue_message() */
1979 ****************************************************************************
1981 * Driver instantiation
1983 ****************************************************************************
1985 static struct file_operations unifi_fops = {
1986 .owner = THIS_MODULE,
1988 .release = unifi_release,
1990 .write = unifi_write,
1991 .unlocked_ioctl = unifi_ioctl,
1995 static dev_t unifi_first_devno;
1996 static struct class *unifi_class;
1999 int uf_create_device_nodes(unifi_priv_t *priv, int bus_id)
2004 cdev_init(&priv->unifi_cdev, &unifi_fops);
2006 /* cdev_init() should set the cdev owner, but it does not */
2007 priv->unifi_cdev.owner = THIS_MODULE;
2009 devno = MKDEV(MAJOR(unifi_first_devno),
2010 MINOR(unifi_first_devno) + (bus_id * 2));
2011 r = cdev_add(&priv->unifi_cdev, devno, 1);
2016 #ifdef SDIO_EXPORTS_STRUCT_DEVICE
2017 if (!device_create(unifi_class, priv->unifi_device,
2018 devno, priv, "unifi%d", bus_id)) {
2020 priv->unifi_device = device_create(unifi_class, NULL,
2021 devno, priv, "unifi%d", bus_id);
2022 if (priv->unifi_device == NULL) {
2023 #endif /* SDIO_EXPORTS_STRUCT_DEVICE */
2025 cdev_del(&priv->unifi_cdev);
2029 cdev_init(&priv->unifiudi_cdev, &unifi_fops);
2031 /* cdev_init() should set the cdev owner, but it does not */
2032 priv->unifiudi_cdev.owner = THIS_MODULE;
2034 devno = MKDEV(MAJOR(unifi_first_devno),
2035 MINOR(unifi_first_devno) + (bus_id * 2) + 1);
2036 r = cdev_add(&priv->unifiudi_cdev, devno, 1);
2038 device_destroy(unifi_class, priv->unifi_cdev.dev);
2039 cdev_del(&priv->unifi_cdev);
2043 if (!device_create(unifi_class,
2044 #ifdef SDIO_EXPORTS_STRUCT_DEVICE
2048 #endif /* SDIO_EXPORTS_STRUCT_DEVICE */
2049 devno, priv, "unifiudi%d", bus_id)) {
2050 device_destroy(unifi_class, priv->unifi_cdev.dev);
2051 cdev_del(&priv->unifiudi_cdev);
2052 cdev_del(&priv->unifi_cdev);
2060 void uf_destroy_device_nodes(unifi_priv_t *priv)
2062 device_destroy(unifi_class, priv->unifiudi_cdev.dev);
2063 device_destroy(unifi_class, priv->unifi_cdev.dev);
2064 cdev_del(&priv->unifiudi_cdev);
2065 cdev_del(&priv->unifi_cdev);
2071 * ----------------------------------------------------------------
2072 * uf_create_debug_device
2074 * Allocates device numbers for unifi character device nodes
2075 * and creates a unifi class in sysfs
2078 * fops Pointer to the char device operations structure.
2081 * 0 on success, -ve error code on error.
2082 * ----------------------------------------------------------------
2085 uf_create_debug_device(struct file_operations *fops)
2089 /* Allocate two device numbers for each device. */
2090 ret = alloc_chrdev_region(&unifi_first_devno, 0, MAX_UNIFI_DEVS*2, UNIFI_NAME);
2092 unifi_error(NULL, "Failed to add alloc dev numbers: %d\n", ret);
2096 /* Create a UniFi class */
2097 unifi_class = class_create(THIS_MODULE, UNIFI_NAME);
2098 if (IS_ERR(unifi_class)) {
2099 unifi_error(NULL, "Failed to create UniFi class\n");
2101 /* Release device numbers */
2102 unregister_chrdev_region(unifi_first_devno, MAX_UNIFI_DEVS*2);
2103 unifi_first_devno = 0;
2108 } /* uf_create_debug_device() */
2112 * ----------------------------------------------------------------
2113 * uf_remove_debug_device
2115 * Destroys the unifi class and releases the allocated
2116 * device numbers for unifi character device nodes.
2121 * ----------------------------------------------------------------
2124 uf_remove_debug_device(void)
2126 /* Destroy the UniFi class */
2127 class_destroy(unifi_class);
2129 /* Release device numbers */
2130 unregister_chrdev_region(unifi_first_devno, MAX_UNIFI_DEVS*2);
2131 unifi_first_devno = 0;
2133 } /* uf_remove_debug_device() */
2137 * ---------------------------------------------------------------------------
2141 * ---------------------------------------------------------------------------
2148 printk("UniFi SDIO Driver: %s %s %s\n",
2150 __DATE__, __TIME__);
2152 #ifdef CSR_SME_USERSPACE
2153 #ifdef CSR_SUPPORT_WEXT
2154 printk("CSR SME with WEXT support\n");
2156 printk("CSR SME no WEXT support\n");
2157 #endif /* CSR_SUPPORT_WEXT */
2158 #endif /* CSR_SME_USERSPACE */
2160 #ifdef CSR_NATIVE_LINUX
2161 #ifdef CSR_SUPPORT_WEXT
2162 #error WEXT unsupported in the native driver
2164 printk("CSR native no WEXT support\n");
2166 #ifdef CSR_WIFI_SPLIT_PATCH
2167 printk("Split patch support\n");
2169 printk("Kernel %d.%d.%d\n",
2170 ((LINUX_VERSION_CODE) >> 16) & 0xff,
2171 ((LINUX_VERSION_CODE) >> 8) & 0xff,
2172 (LINUX_VERSION_CODE) & 0xff);
2174 * Instantiate the /dev/unifi* device nodes.
2175 * We must do this before registering with the SDIO driver because it
2176 * will immediately call the "insert" callback if the card is
2179 r = uf_create_debug_device(&unifi_fops);
2184 /* Now register with the SDIO driver */
2187 uf_remove_debug_device();
2191 if (sdio_block_size > -1) {
2192 unifi_info(NULL, "sdio_block_size %d\n", sdio_block_size);
2195 if (sdio_byte_mode) {
2196 unifi_info(NULL, "sdio_byte_mode\n");
2199 if (disable_power_control) {
2200 unifi_info(NULL, "disable_power_control\n");
2203 if (disable_hw_reset) {
2204 unifi_info(NULL, "disable_hw_reset\n");
2208 unifi_info(NULL, "enable_wol %d\n", enable_wol);
2211 if (run_bh_once != -1) {
2212 unifi_info(NULL, "run_bh_once %d\n", run_bh_once);
2216 } /* unifi_load() */
2222 /* The SDIO remove hook will call unifi_disconnect(). */
2225 uf_remove_debug_device();
2227 } /* unifi_unload() */
2229 module_init(unifi_load);
2230 module_exit(unifi_unload);
2232 MODULE_DESCRIPTION("UniFi Device driver");
2233 MODULE_AUTHOR("Cambridge Silicon Radio Ltd.");
2234 MODULE_LICENSE("GPL and additional rights");