2 * http://www.cascoda.com/products/ca-821x/
3 * Copyright (c) 2016, Cascoda, Ltd.
6 * This code is dual-licensed under both GPLv2 and 3-clause BSD. What follows is
7 * the license notice for both respectively.
9 *******************************************************************************
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 *******************************************************************************
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are met:
26 * 1. Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright notice,
30 * this list of conditions and the following disclaimer in the documentation
31 * and/or other materials provided with the distribution.
33 * 3. Neither the name of the copyright holder nor the names of its contributors
34 * may be used to endorse or promote products derived from this software without
35 * specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
41 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
50 #include <linux/cdev.h>
51 #include <linux/clk-provider.h>
52 #include <linux/debugfs.h>
53 #include <linux/delay.h>
54 #include <linux/gpio/consumer.h>
55 #include <linux/gpio.h>
56 #include <linux/ieee802154.h>
58 #include <linux/kfifo.h>
60 #include <linux/of_device.h>
61 #include <linux/of_gpio.h>
62 #include <linux/module.h>
63 #include <linux/mutex.h>
64 #include <linux/poll.h>
65 #include <linux/skbuff.h>
66 #include <linux/slab.h>
67 #include <linux/spi/spi.h>
68 #include <linux/spinlock.h>
69 #include <linux/string.h>
70 #include <linux/workqueue.h>
71 #include <linux/interrupt.h>
73 #include <net/ieee802154_netdev.h>
74 #include <net/mac802154.h>
76 #define DRIVER_NAME "ca8210"
78 /* external clock frequencies */
79 #define ONE_MHZ 1000000
80 #define TWO_MHZ (2 * ONE_MHZ)
81 #define FOUR_MHZ (4 * ONE_MHZ)
82 #define EIGHT_MHZ (8 * ONE_MHZ)
83 #define SIXTEEN_MHZ (16 * ONE_MHZ)
86 #define CA8210_SPI_BUF_SIZE 256
87 #define CA8210_SYNC_TIMEOUT 1000 /* Timeout for synchronous commands [ms] */
89 /* test interface constants */
90 #define CA8210_TEST_INT_FILE_NAME "ca8210_test"
91 #define CA8210_TEST_INT_FIFO_SIZE 256
93 /* HWME attribute IDs */
94 #define HWME_EDTHRESHOLD (0x04)
95 #define HWME_EDVALUE (0x06)
96 #define HWME_SYSCLKOUT (0x0F)
97 #define HWME_LQILIMIT (0x11)
99 /* TDME attribute IDs */
100 #define TDME_CHANNEL (0x00)
101 #define TDME_ATM_CONFIG (0x06)
103 #define MAX_HWME_ATTRIBUTE_SIZE 16
104 #define MAX_TDME_ATTRIBUTE_SIZE 2
106 /* PHY/MAC PIB Attribute Enumerations */
107 #define PHY_CURRENT_CHANNEL (0x00)
108 #define PHY_TRANSMIT_POWER (0x02)
109 #define PHY_CCA_MODE (0x03)
110 #define MAC_ASSOCIATION_PERMIT (0x41)
111 #define MAC_AUTO_REQUEST (0x42)
112 #define MAC_BATT_LIFE_EXT (0x43)
113 #define MAC_BATT_LIFE_EXT_PERIODS (0x44)
114 #define MAC_BEACON_PAYLOAD (0x45)
115 #define MAC_BEACON_PAYLOAD_LENGTH (0x46)
116 #define MAC_BEACON_ORDER (0x47)
117 #define MAC_GTS_PERMIT (0x4d)
118 #define MAC_MAX_CSMA_BACKOFFS (0x4e)
119 #define MAC_MIN_BE (0x4f)
120 #define MAC_PAN_ID (0x50)
121 #define MAC_PROMISCUOUS_MODE (0x51)
122 #define MAC_RX_ON_WHEN_IDLE (0x52)
123 #define MAC_SHORT_ADDRESS (0x53)
124 #define MAC_SUPERFRAME_ORDER (0x54)
125 #define MAC_ASSOCIATED_PAN_COORD (0x56)
126 #define MAC_MAX_BE (0x57)
127 #define MAC_MAX_FRAME_RETRIES (0x59)
128 #define MAC_RESPONSE_WAIT_TIME (0x5A)
129 #define MAC_SECURITY_ENABLED (0x5D)
131 #define MAC_AUTO_REQUEST_SECURITY_LEVEL (0x78)
132 #define MAC_AUTO_REQUEST_KEY_ID_MODE (0x79)
134 #define NS_IEEE_ADDRESS (0xFF) /* Non-standard IEEE address */
136 /* MAC Address Mode Definitions */
137 #define MAC_MODE_NO_ADDR (0x00)
138 #define MAC_MODE_SHORT_ADDR (0x02)
139 #define MAC_MODE_LONG_ADDR (0x03)
142 #define MAX_BEACON_OVERHEAD (75)
143 #define MAX_BEACON_PAYLOAD_LENGTH (IEEE802154_MTU - MAX_BEACON_OVERHEAD)
145 #define MAX_ATTRIBUTE_SIZE (122)
146 #define MAX_DATA_SIZE (114)
148 #define CA8210_VALID_CHANNELS (0x07FFF800)
150 /* MAC workarounds for V1.1 and MPW silicon (V0.x) */
151 #define CA8210_MAC_WORKAROUNDS (0)
152 #define CA8210_MAC_MPW (0)
154 /* memory manipulation macros */
155 #define LS_BYTE(x) ((u8)((x) & 0xFF))
156 #define MS_BYTE(x) ((u8)(((x) >> 8) & 0xFF))
158 /* message ID codes in SPI commands */
160 #define MCPS_DATA_REQUEST (0x00)
161 #define MLME_ASSOCIATE_REQUEST (0x02)
162 #define MLME_ASSOCIATE_RESPONSE (0x03)
163 #define MLME_DISASSOCIATE_REQUEST (0x04)
164 #define MLME_GET_REQUEST (0x05)
165 #define MLME_ORPHAN_RESPONSE (0x06)
166 #define MLME_RESET_REQUEST (0x07)
167 #define MLME_RX_ENABLE_REQUEST (0x08)
168 #define MLME_SCAN_REQUEST (0x09)
169 #define MLME_SET_REQUEST (0x0A)
170 #define MLME_START_REQUEST (0x0B)
171 #define MLME_POLL_REQUEST (0x0D)
172 #define HWME_SET_REQUEST (0x0E)
173 #define HWME_GET_REQUEST (0x0F)
174 #define TDME_SETSFR_REQUEST (0x11)
175 #define TDME_GETSFR_REQUEST (0x12)
176 #define TDME_SET_REQUEST (0x14)
178 #define MCPS_DATA_INDICATION (0x00)
179 #define MCPS_DATA_CONFIRM (0x01)
180 #define MLME_RESET_CONFIRM (0x0A)
181 #define MLME_SET_CONFIRM (0x0E)
182 #define MLME_START_CONFIRM (0x0F)
183 #define HWME_SET_CONFIRM (0x12)
184 #define HWME_GET_CONFIRM (0x13)
185 #define HWME_WAKEUP_INDICATION (0x15)
186 #define TDME_SETSFR_CONFIRM (0x17)
188 /* SPI command IDs */
189 /* bit indicating a confirm or indication from slave to master */
190 #define SPI_S2M (0x20)
191 /* bit indicating a synchronous message */
192 #define SPI_SYN (0x40)
194 /* SPI command definitions */
195 #define SPI_IDLE (0xFF)
196 #define SPI_NACK (0xF0)
198 #define SPI_MCPS_DATA_REQUEST (MCPS_DATA_REQUEST)
199 #define SPI_MCPS_DATA_INDICATION (MCPS_DATA_INDICATION + SPI_S2M)
200 #define SPI_MCPS_DATA_CONFIRM (MCPS_DATA_CONFIRM + SPI_S2M)
202 #define SPI_MLME_ASSOCIATE_REQUEST (MLME_ASSOCIATE_REQUEST)
203 #define SPI_MLME_RESET_REQUEST (MLME_RESET_REQUEST + SPI_SYN)
204 #define SPI_MLME_SET_REQUEST (MLME_SET_REQUEST + SPI_SYN)
205 #define SPI_MLME_START_REQUEST (MLME_START_REQUEST + SPI_SYN)
206 #define SPI_MLME_RESET_CONFIRM (MLME_RESET_CONFIRM + SPI_S2M + SPI_SYN)
207 #define SPI_MLME_SET_CONFIRM (MLME_SET_CONFIRM + SPI_S2M + SPI_SYN)
208 #define SPI_MLME_START_CONFIRM (MLME_START_CONFIRM + SPI_S2M + SPI_SYN)
210 #define SPI_HWME_SET_REQUEST (HWME_SET_REQUEST + SPI_SYN)
211 #define SPI_HWME_GET_REQUEST (HWME_GET_REQUEST + SPI_SYN)
212 #define SPI_HWME_SET_CONFIRM (HWME_SET_CONFIRM + SPI_S2M + SPI_SYN)
213 #define SPI_HWME_GET_CONFIRM (HWME_GET_CONFIRM + SPI_S2M + SPI_SYN)
214 #define SPI_HWME_WAKEUP_INDICATION (HWME_WAKEUP_INDICATION + SPI_S2M)
216 #define SPI_TDME_SETSFR_REQUEST (TDME_SETSFR_REQUEST + SPI_SYN)
217 #define SPI_TDME_SET_REQUEST (TDME_SET_REQUEST + SPI_SYN)
218 #define SPI_TDME_SETSFR_CONFIRM (TDME_SETSFR_CONFIRM + SPI_S2M + SPI_SYN)
220 /* TDME SFR addresses */
222 #define CA8210_SFR_PACFG (0xB1)
223 #define CA8210_SFR_MACCON (0xD8)
224 #define CA8210_SFR_PACFGIB (0xFE)
226 #define CA8210_SFR_LOTXCAL (0xBF)
227 #define CA8210_SFR_PTHRH (0xD1)
228 #define CA8210_SFR_PRECFG (0xD3)
229 #define CA8210_SFR_LNAGX40 (0xE1)
230 #define CA8210_SFR_LNAGX41 (0xE2)
231 #define CA8210_SFR_LNAGX42 (0xE3)
232 #define CA8210_SFR_LNAGX43 (0xE4)
233 #define CA8210_SFR_LNAGX44 (0xE5)
234 #define CA8210_SFR_LNAGX45 (0xE6)
235 #define CA8210_SFR_LNAGX46 (0xE7)
236 #define CA8210_SFR_LNAGX47 (0xE9)
238 #define PACFGIB_DEFAULT_CURRENT (0x3F)
239 #define PTHRH_DEFAULT_THRESHOLD (0x5A)
240 #define LNAGX40_DEFAULT_GAIN (0x29) /* 10dB */
241 #define LNAGX41_DEFAULT_GAIN (0x54) /* 21dB */
242 #define LNAGX42_DEFAULT_GAIN (0x6C) /* 27dB */
243 #define LNAGX43_DEFAULT_GAIN (0x7A) /* 30dB */
244 #define LNAGX44_DEFAULT_GAIN (0x84) /* 33dB */
245 #define LNAGX45_DEFAULT_GAIN (0x8B) /* 34dB */
246 #define LNAGX46_DEFAULT_GAIN (0x92) /* 36dB */
247 #define LNAGX47_DEFAULT_GAIN (0x96) /* 37dB */
249 #define CA8210_IOCTL_HARD_RESET (0x00)
254 * struct cas_control - spi transfer structure
255 * @msg: spi_message for each exchange
256 * @transfer: spi_transfer for each exchange
257 * @tx_buf: source array for transmission
258 * @tx_in_buf: array storing bytes received during transmission
259 * @priv: pointer to private data
261 * This structure stores all the necessary data passed around during a single
265 struct spi_message msg;
266 struct spi_transfer transfer;
268 u8 tx_buf[CA8210_SPI_BUF_SIZE];
269 u8 tx_in_buf[CA8210_SPI_BUF_SIZE];
271 struct ca8210_priv *priv;
275 * struct ca8210_test - ca8210 test interface structure
276 * @ca8210_dfs_spi_int: pointer to the entry in the debug fs for this device
277 * @up_fifo: fifo for upstream messages
278 * @readq: read wait queue
280 * This structure stores all the data pertaining to the debug interface
283 struct dentry *ca8210_dfs_spi_int;
284 struct kfifo up_fifo;
285 wait_queue_head_t readq;
289 * struct ca8210_priv - ca8210 private data structure
290 * @spi: pointer to the ca8210 spi device object
291 * @hw: pointer to the ca8210 ieee802154_hw object
292 * @hw_registered: true if hw has been registered with ieee802154
293 * @lock: spinlock protecting the private data area
294 * @mlme_workqueue: workqueue for triggering MLME Reset
295 * @irq_workqueue: workqueue for irq processing
296 * @tx_skb: current socket buffer to transmit
297 * @nextmsduhandle: msdu handle to pass to the 15.4 MAC layer for the
299 * @clk: external clock provided by the ca8210
300 * @last_dsn: sequence number of last data packet received, for
302 * @test: test interface data section for this instance
303 * @async_tx_pending: true if an asynchronous transmission was started and
305 * @sync_command_response: pointer to buffer to fill with sync response
306 * @ca8210_is_awake: nonzero if ca8210 is initialised, ready for comms
307 * @sync_down: counts number of downstream synchronous commands
308 * @sync_up: counts number of upstream synchronous commands
309 * @spi_transfer_complete: completion object for a single spi_transfer
310 * @sync_exchange_complete: completion object for a complete synchronous API
312 * @promiscuous: whether the ca8210 is in promiscuous mode or not
313 * @retries: records how many times the current pending spi
314 * transfer has been retried
317 struct spi_device *spi;
318 struct ieee802154_hw *hw;
321 struct workqueue_struct *mlme_workqueue;
322 struct workqueue_struct *irq_workqueue;
323 struct sk_buff *tx_skb;
327 struct ca8210_test test;
328 bool async_tx_pending;
329 u8 *sync_command_response;
330 struct completion ca8210_is_awake;
331 int sync_down, sync_up;
332 struct completion spi_transfer_complete, sync_exchange_complete;
338 * struct work_priv_container - link between a work object and the relevant
339 * device's private data
340 * @work: work object being executed
341 * @priv: device's private data section
344 struct work_priv_container {
345 struct work_struct work;
346 struct ca8210_priv *priv;
350 * struct ca8210_platform_data - ca8210 platform data structure
351 * @extclockenable: true if the external clock is to be enabled
352 * @extclockfreq: frequency of the external clock
353 * @extclockgpio: ca8210 output gpio of the external clock
354 * @gpio_reset: gpio number of ca8210 reset line
355 * @gpio_irq: gpio number of ca8210 interrupt line
356 * @irq_id: identifier for the ca8210 irq
359 struct ca8210_platform_data {
361 unsigned int extclockfreq;
362 unsigned int extclockgpio;
369 * struct fulladdr - full MAC addressing information structure
370 * @mode: address mode (none, short, extended)
371 * @pan_id: 16-bit LE pan id
372 * @address: LE address, variable length as specified by mode
382 * union macaddr: generic MAC address container
383 * @short_address: 16-bit short address
384 * @ieee_address: 64-bit extended address as LE byte array
393 * struct secspec: security specification for SAP commands
394 * @security_level: 0-7, controls level of authentication & encryption
395 * @key_id_mode: 0-3, specifies how to obtain key
396 * @key_source: extended key retrieval data
397 * @key_index: single-byte key identifier
407 /* downlink functions parameter set definitions */
408 struct mcps_data_request_pset {
414 u8 msdu[MAX_DATA_SIZE];
417 struct mlme_set_request_pset {
419 u8 pib_attribute_index;
420 u8 pib_attribute_length;
421 u8 pib_attribute_value[MAX_ATTRIBUTE_SIZE];
424 struct hwme_set_request_pset {
426 u8 hw_attribute_length;
427 u8 hw_attribute_value[MAX_HWME_ATTRIBUTE_SIZE];
430 struct hwme_get_request_pset {
434 struct tdme_setsfr_request_pset {
440 /* uplink functions parameter set definitions */
441 struct hwme_set_confirm_pset {
446 struct hwme_get_confirm_pset {
449 u8 hw_attribute_length;
450 u8 hw_attribute_value[MAX_HWME_ATTRIBUTE_SIZE];
453 struct tdme_setsfr_confirm_pset {
463 struct mcps_data_request_pset data_req;
464 struct mlme_set_request_pset set_req;
465 struct hwme_set_request_pset hwme_set_req;
466 struct hwme_get_request_pset hwme_get_req;
467 struct tdme_setsfr_request_pset tdme_set_sfr_req;
468 struct hwme_set_confirm_pset hwme_set_cnf;
469 struct hwme_get_confirm_pset hwme_get_cnf;
470 struct tdme_setsfr_confirm_pset tdme_set_sfr_cnf;
479 u8 bias_current_trim : 3;
480 u8 /* reserved */ : 1;
481 u8 buffer_capacitor_trim : 3;
487 struct preamble_cfg_sfr {
488 u8 timeout_symbols : 3;
489 u8 acquisition_symbols : 3;
490 u8 search_symbols : 2;
493 static int (*cascoda_api_upstream)(
500 * link_to_linux_err() - Translates an 802.15.4 return code into the closest
502 * @link_status: 802.15.4 status code
504 * Return: 0 or Linux error code
506 static int link_to_linux_err(int link_status)
508 if (link_status < 0) {
509 /* status is already a Linux code */
512 switch (link_status) {
513 case IEEE802154_SUCCESS:
514 case IEEE802154_REALIGNMENT:
516 case IEEE802154_IMPROPER_KEY_TYPE:
517 return -EKEYREJECTED;
518 case IEEE802154_IMPROPER_SECURITY_LEVEL:
519 case IEEE802154_UNSUPPORTED_LEGACY:
520 case IEEE802154_DENIED:
522 case IEEE802154_BEACON_LOST:
523 case IEEE802154_NO_ACK:
524 case IEEE802154_NO_BEACON:
526 case IEEE802154_CHANNEL_ACCESS_FAILURE:
527 case IEEE802154_TX_ACTIVE:
528 case IEEE802154_SCAN_IN_PROGRESS:
530 case IEEE802154_DISABLE_TRX_FAILURE:
531 case IEEE802154_OUT_OF_CAP:
533 case IEEE802154_FRAME_TOO_LONG:
535 case IEEE802154_INVALID_GTS:
536 case IEEE802154_PAST_TIME:
538 case IEEE802154_INVALID_HANDLE:
540 case IEEE802154_INVALID_PARAMETER:
541 case IEEE802154_UNSUPPORTED_ATTRIBUTE:
542 case IEEE802154_ON_TIME_TOO_LONG:
543 case IEEE802154_INVALID_INDEX:
545 case IEEE802154_NO_DATA:
547 case IEEE802154_NO_SHORT_ADDRESS:
549 case IEEE802154_PAN_ID_CONFLICT:
551 case IEEE802154_TRANSACTION_EXPIRED:
553 case IEEE802154_TRANSACTION_OVERFLOW:
555 case IEEE802154_UNAVAILABLE_KEY:
557 case IEEE802154_INVALID_ADDRESS:
559 case IEEE802154_TRACKING_OFF:
560 case IEEE802154_SUPERFRAME_OVERLAP:
562 case IEEE802154_LIMIT_REACHED:
564 case IEEE802154_READ_ONLY:
572 * ca8210_test_int_driver_write() - Writes a message to the test interface to be
573 * read by the userspace
574 * @buf: Buffer containing upstream message
575 * @len: length of message to write
576 * @spi: SPI device of message originator
578 * Return: 0 or linux error code
580 static int ca8210_test_int_driver_write(
586 struct ca8210_priv *priv = spi_get_drvdata(spi);
587 struct ca8210_test *test = &priv->test;
593 "test_interface: Buffering upstream message:\n"
595 for (i = 0; i < len; i++)
596 dev_dbg(&priv->spi->dev, "%#03x\n", buf[i]);
598 fifo_buffer = kmemdup(buf, len, GFP_KERNEL);
601 kfifo_in(&test->up_fifo, &fifo_buffer, 4);
602 wake_up_interruptible(&priv->test.readq);
609 static int ca8210_net_rx(
610 struct ieee802154_hw *hw,
614 static u8 mlme_reset_request_sync(
618 static int ca8210_spi_transfer(
619 struct spi_device *spi,
625 * ca8210_reset_send() - Hard resets the ca8210 for a given time
626 * @spi: Pointer to target ca8210 spi device
627 * @ms: Milliseconds to hold the reset line low for
629 static void ca8210_reset_send(struct spi_device *spi, unsigned int ms)
631 struct ca8210_platform_data *pdata = spi->dev.platform_data;
632 struct ca8210_priv *priv = spi_get_drvdata(spi);
635 gpio_set_value(pdata->gpio_reset, 0);
636 reinit_completion(&priv->ca8210_is_awake);
638 gpio_set_value(pdata->gpio_reset, 1);
639 priv->promiscuous = false;
641 /* Wait until wakeup indication seen */
642 status = wait_for_completion_interruptible_timeout(
643 &priv->ca8210_is_awake,
644 msecs_to_jiffies(CA8210_SYNC_TIMEOUT)
649 "Fatal: No wakeup from ca8210 after reset!\n"
653 dev_dbg(&spi->dev, "Reset the device\n");
657 * ca8210_mlme_reset_worker() - Resets the MLME, Called when the MAC OVERFLOW
659 * @work: Pointer to work being executed
661 static void ca8210_mlme_reset_worker(struct work_struct *work)
663 struct work_priv_container *wpc = container_of(
665 struct work_priv_container,
668 struct ca8210_priv *priv = wpc->priv;
670 mlme_reset_request_sync(0, priv->spi);
675 * ca8210_rx_done() - Calls various message dispatches responding to a received
677 * @cas_ctl: Pointer to the cas_control object for the relevant spi transfer
679 * Presents a received SAP command from the ca8210 to the Cascoda EVBME, test
680 * interface and network driver.
682 static void ca8210_rx_done(struct cas_control *cas_ctl)
686 struct work_priv_container *mlme_reset_wpc;
687 struct ca8210_priv *priv = cas_ctl->priv;
689 buf = cas_ctl->tx_in_buf;
691 if (len > CA8210_SPI_BUF_SIZE) {
694 "Received packet len (%u) erroneously long\n",
700 if (buf[0] & SPI_SYN) {
701 if (priv->sync_command_response) {
702 memcpy(priv->sync_command_response, buf, len);
703 complete(&priv->sync_exchange_complete);
705 if (cascoda_api_upstream)
706 cascoda_api_upstream(buf, len, priv->spi);
710 if (cascoda_api_upstream)
711 cascoda_api_upstream(buf, len, priv->spi);
714 ca8210_net_rx(priv->hw, buf, len);
715 if (buf[0] == SPI_MCPS_DATA_CONFIRM) {
716 if (buf[3] == IEEE802154_TRANSACTION_OVERFLOW) {
719 "Waiting for transaction overflow to stabilise...\n");
723 "Resetting MAC...\n");
725 mlme_reset_wpc = kmalloc(sizeof(*mlme_reset_wpc),
730 &mlme_reset_wpc->work,
731 ca8210_mlme_reset_worker
733 mlme_reset_wpc->priv = priv;
734 queue_work(priv->mlme_workqueue, &mlme_reset_wpc->work);
736 } else if (buf[0] == SPI_HWME_WAKEUP_INDICATION) {
739 "Wakeup indication received, reason:\n"
745 "Transceiver woken up from Power Up / System Reset\n"
751 "Watchdog Timer Time-Out\n"
757 "Transceiver woken up from Power-Off by Sleep Timer Time-Out\n");
762 "Transceiver woken up from Power-Off by GPIO Activity\n"
768 "Transceiver woken up from Standby by Sleep Timer Time-Out\n"
774 "Transceiver woken up from Standby by GPIO Activity\n"
780 "Sleep-Timer Time-Out in Active Mode\n"
784 dev_warn(&priv->spi->dev, "Wakeup reason unknown\n");
787 complete(&priv->ca8210_is_awake);
793 static void ca8210_remove(struct spi_device *spi_device);
796 * ca8210_spi_transfer_complete() - Called when a single spi transfer has
798 * @context: Pointer to the cas_control object for the finished transfer
800 static void ca8210_spi_transfer_complete(void *context)
802 struct cas_control *cas_ctl = context;
803 struct ca8210_priv *priv = cas_ctl->priv;
804 bool duplex_rx = false;
806 u8 retry_buffer[CA8210_SPI_BUF_SIZE];
809 cas_ctl->tx_in_buf[0] == SPI_NACK ||
810 (cas_ctl->tx_in_buf[0] == SPI_IDLE &&
811 cas_ctl->tx_in_buf[1] == SPI_NACK)
814 dev_info(&priv->spi->dev, "ca8210 was busy during attempted write\n");
815 if (cas_ctl->tx_buf[0] == SPI_IDLE) {
818 "IRQ servicing NACKd, dropping transfer\n"
823 if (priv->retries > 3) {
824 dev_err(&priv->spi->dev, "too many retries!\n");
826 ca8210_remove(priv->spi);
829 memcpy(retry_buffer, cas_ctl->tx_buf, CA8210_SPI_BUF_SIZE);
837 dev_info(&priv->spi->dev, "retried spi write\n");
840 cas_ctl->tx_in_buf[0] != SPI_IDLE &&
841 cas_ctl->tx_in_buf[0] != SPI_NACK
847 dev_dbg(&priv->spi->dev, "READ CMD DURING TX\n");
848 for (i = 0; i < cas_ctl->tx_in_buf[1] + 2; i++)
852 cas_ctl->tx_in_buf[i]
854 ca8210_rx_done(cas_ctl);
856 complete(&priv->spi_transfer_complete);
862 * ca8210_spi_transfer() - Initiate duplex spi transfer with ca8210
863 * @spi: Pointer to spi device for transfer
864 * @buf: Octet array to send
865 * @len: length of the buffer being sent
867 * Return: 0 or linux error code
869 static int ca8210_spi_transfer(
870 struct spi_device *spi,
876 struct ca8210_priv *priv;
877 struct cas_control *cas_ctl;
880 pr_crit("NULL spi device passed to %s\n", __func__);
884 priv = spi_get_drvdata(spi);
885 reinit_completion(&priv->spi_transfer_complete);
887 dev_dbg(&spi->dev, "%s called\n", __func__);
889 cas_ctl = kzalloc(sizeof(*cas_ctl), GFP_ATOMIC);
893 cas_ctl->priv = priv;
894 memset(cas_ctl->tx_buf, SPI_IDLE, CA8210_SPI_BUF_SIZE);
895 memset(cas_ctl->tx_in_buf, SPI_IDLE, CA8210_SPI_BUF_SIZE);
896 memcpy(cas_ctl->tx_buf, buf, len);
898 for (i = 0; i < len; i++)
899 dev_dbg(&spi->dev, "%#03x\n", cas_ctl->tx_buf[i]);
901 spi_message_init(&cas_ctl->msg);
903 cas_ctl->transfer.tx_nbits = 1; /* 1 MOSI line */
904 cas_ctl->transfer.rx_nbits = 1; /* 1 MISO line */
905 cas_ctl->transfer.speed_hz = 0; /* Use device setting */
906 cas_ctl->transfer.bits_per_word = 0; /* Use device setting */
907 cas_ctl->transfer.tx_buf = cas_ctl->tx_buf;
908 cas_ctl->transfer.rx_buf = cas_ctl->tx_in_buf;
909 cas_ctl->transfer.delay.value = 0;
910 cas_ctl->transfer.delay.unit = SPI_DELAY_UNIT_USECS;
911 cas_ctl->transfer.cs_change = 0;
912 cas_ctl->transfer.len = sizeof(struct mac_message);
913 cas_ctl->msg.complete = ca8210_spi_transfer_complete;
914 cas_ctl->msg.context = cas_ctl;
916 spi_message_add_tail(
921 status = spi_async(spi, &cas_ctl->msg);
925 "status %d from spi_sync in write\n",
934 * ca8210_spi_exchange() - Exchange API/SAP commands with the radio
935 * @buf: Octet array of command being sent downstream
936 * @len: length of buf
937 * @response: buffer for storing synchronous response
938 * @device_ref: spi_device pointer for ca8210
940 * Effectively calls ca8210_spi_transfer to write buf[] to the spi, then for
941 * synchronous commands waits for the corresponding response to be read from
942 * the spi before returning. The response is written to the response parameter.
944 * Return: 0 or linux error code
946 static int ca8210_spi_exchange(
954 struct spi_device *spi = device_ref;
955 struct ca8210_priv *priv = spi->dev.driver_data;
958 if ((buf[0] & SPI_SYN) && response) { /* if sync wait for confirm */
959 reinit_completion(&priv->sync_exchange_complete);
960 priv->sync_command_response = response;
964 reinit_completion(&priv->spi_transfer_complete);
965 status = ca8210_spi_transfer(priv->spi, buf, len);
969 "spi write failed, returned %d\n",
972 if (status == -EBUSY)
974 if (((buf[0] & SPI_SYN) && response))
975 complete(&priv->sync_exchange_complete);
979 wait_remaining = wait_for_completion_interruptible_timeout(
980 &priv->spi_transfer_complete,
981 msecs_to_jiffies(1000)
983 if (wait_remaining == -ERESTARTSYS) {
984 status = -ERESTARTSYS;
985 } else if (wait_remaining == 0) {
988 "SPI downstream transfer timed out!\n"
993 } while (status < 0);
995 if (!((buf[0] & SPI_SYN) && response))
998 wait_remaining = wait_for_completion_interruptible_timeout(
999 &priv->sync_exchange_complete,
1000 msecs_to_jiffies(CA8210_SYNC_TIMEOUT)
1002 if (wait_remaining == -ERESTARTSYS) {
1003 status = -ERESTARTSYS;
1004 } else if (wait_remaining == 0) {
1007 "Synchronous confirm timeout\n"
1013 priv->sync_command_response = NULL;
1018 * ca8210_interrupt_handler() - Called when an irq is received from the ca8210
1019 * @irq: Id of the irq being handled
1020 * @dev_id: Pointer passed by the system, pointing to the ca8210's private data
1022 * This function is called when the irq line from the ca8210 is asserted,
1023 * signifying that the ca8210 has a message to send upstream to us. Starts the
1024 * asynchronous spi read.
1026 * Return: irq return code
1028 static irqreturn_t ca8210_interrupt_handler(int irq, void *dev_id)
1030 struct ca8210_priv *priv = dev_id;
1033 dev_dbg(&priv->spi->dev, "irq: Interrupt occurred\n");
1035 status = ca8210_spi_transfer(priv->spi, NULL, 0);
1036 if (status && (status != -EBUSY)) {
1039 "spi read failed, returned %d\n",
1043 } while (status == -EBUSY);
1047 static int (*cascoda_api_downstream)(
1052 ) = ca8210_spi_exchange;
1054 /* Cascoda API / 15.4 SAP Primitives */
1057 * tdme_setsfr_request_sync() - TDME_SETSFR_request/confirm according to API
1058 * @sfr_page: SFR Page
1059 * @sfr_address: SFR Address
1060 * @sfr_value: SFR Value
1061 * @device_ref: Nondescript pointer to target device
1063 * Return: 802.15.4 status code of TDME-SETSFR.confirm
1065 static u8 tdme_setsfr_request_sync(
1073 struct mac_message command, response;
1074 struct spi_device *spi = device_ref;
1076 command.command_id = SPI_TDME_SETSFR_REQUEST;
1078 command.pdata.tdme_set_sfr_req.sfr_page = sfr_page;
1079 command.pdata.tdme_set_sfr_req.sfr_address = sfr_address;
1080 command.pdata.tdme_set_sfr_req.sfr_value = sfr_value;
1081 response.command_id = SPI_IDLE;
1082 ret = cascoda_api_downstream(
1083 &command.command_id,
1085 &response.command_id,
1089 dev_crit(&spi->dev, "cascoda_api_downstream returned %d", ret);
1090 return IEEE802154_SYSTEM_ERROR;
1093 if (response.command_id != SPI_TDME_SETSFR_CONFIRM) {
1096 "sync response to SPI_TDME_SETSFR_REQUEST was not SPI_TDME_SETSFR_CONFIRM, it was %d\n",
1099 return IEEE802154_SYSTEM_ERROR;
1102 return response.pdata.tdme_set_sfr_cnf.status;
1106 * tdme_chipinit() - TDME Chip Register Default Initialisation Macro
1107 * @device_ref: Nondescript pointer to target device
1109 * Return: 802.15.4 status code of API calls
1111 static u8 tdme_chipinit(void *device_ref)
1113 u8 status = IEEE802154_SUCCESS;
1115 struct spi_device *spi = device_ref;
1116 struct preamble_cfg_sfr pre_cfg_value = {
1117 .timeout_symbols = 3,
1118 .acquisition_symbols = 3,
1119 .search_symbols = 1,
1121 /* LNA Gain Settings */
1122 status = tdme_setsfr_request_sync(
1123 1, (sfr_address = CA8210_SFR_LNAGX40),
1124 LNAGX40_DEFAULT_GAIN, device_ref);
1127 status = tdme_setsfr_request_sync(
1128 1, (sfr_address = CA8210_SFR_LNAGX41),
1129 LNAGX41_DEFAULT_GAIN, device_ref);
1132 status = tdme_setsfr_request_sync(
1133 1, (sfr_address = CA8210_SFR_LNAGX42),
1134 LNAGX42_DEFAULT_GAIN, device_ref);
1137 status = tdme_setsfr_request_sync(
1138 1, (sfr_address = CA8210_SFR_LNAGX43),
1139 LNAGX43_DEFAULT_GAIN, device_ref);
1142 status = tdme_setsfr_request_sync(
1143 1, (sfr_address = CA8210_SFR_LNAGX44),
1144 LNAGX44_DEFAULT_GAIN, device_ref);
1147 status = tdme_setsfr_request_sync(
1148 1, (sfr_address = CA8210_SFR_LNAGX45),
1149 LNAGX45_DEFAULT_GAIN, device_ref);
1152 status = tdme_setsfr_request_sync(
1153 1, (sfr_address = CA8210_SFR_LNAGX46),
1154 LNAGX46_DEFAULT_GAIN, device_ref);
1157 status = tdme_setsfr_request_sync(
1158 1, (sfr_address = CA8210_SFR_LNAGX47),
1159 LNAGX47_DEFAULT_GAIN, device_ref);
1162 /* Preamble Timing Config */
1163 status = tdme_setsfr_request_sync(
1164 1, (sfr_address = CA8210_SFR_PRECFG),
1165 *((u8 *)&pre_cfg_value), device_ref);
1168 /* Preamble Threshold High */
1169 status = tdme_setsfr_request_sync(
1170 1, (sfr_address = CA8210_SFR_PTHRH),
1171 PTHRH_DEFAULT_THRESHOLD, device_ref);
1174 /* Tx Output Power 8 dBm */
1175 status = tdme_setsfr_request_sync(
1176 0, (sfr_address = CA8210_SFR_PACFGIB),
1177 PACFGIB_DEFAULT_CURRENT, device_ref);
1182 if (status != IEEE802154_SUCCESS) {
1185 "failed to set sfr at %#03x, status = %#03x\n",
1194 * tdme_channelinit() - TDME Channel Register Default Initialisation Macro (Tx)
1195 * @channel: 802.15.4 channel to initialise chip for
1196 * @device_ref: Nondescript pointer to target device
1198 * Return: 802.15.4 status code of API calls
1200 static u8 tdme_channelinit(u8 channel, void *device_ref)
1202 /* Transceiver front-end local oscillator tx two-point calibration
1203 * value. Tuned for the hardware.
1209 else if (channel >= 23)
1211 else if (channel >= 22)
1213 else if (channel >= 20)
1215 else if (channel >= 17)
1217 else if (channel >= 16)
1219 else if (channel >= 14)
1221 else if (channel >= 12)
1226 return tdme_setsfr_request_sync(
1235 * tdme_checkpibattribute() - Checks Attribute Values that are not checked in
1237 * @pib_attribute: Attribute Number
1238 * @pib_attribute_length: Attribute length
1239 * @pib_attribute_value: Pointer to Attribute Value
1241 * Return: 802.15.4 status code of checks
1243 static u8 tdme_checkpibattribute(
1245 u8 pib_attribute_length,
1246 const void *pib_attribute_value
1249 u8 status = IEEE802154_SUCCESS;
1252 value = *((u8 *)pib_attribute_value);
1254 switch (pib_attribute) {
1256 case PHY_TRANSMIT_POWER:
1258 status = IEEE802154_INVALID_PARAMETER;
1262 status = IEEE802154_INVALID_PARAMETER;
1265 case MAC_BATT_LIFE_EXT_PERIODS:
1266 if (value < 6 || value > 41)
1267 status = IEEE802154_INVALID_PARAMETER;
1269 case MAC_BEACON_PAYLOAD:
1270 if (pib_attribute_length > MAX_BEACON_PAYLOAD_LENGTH)
1271 status = IEEE802154_INVALID_PARAMETER;
1273 case MAC_BEACON_PAYLOAD_LENGTH:
1274 if (value > MAX_BEACON_PAYLOAD_LENGTH)
1275 status = IEEE802154_INVALID_PARAMETER;
1277 case MAC_BEACON_ORDER:
1279 status = IEEE802154_INVALID_PARAMETER;
1282 if (value < 3 || value > 8)
1283 status = IEEE802154_INVALID_PARAMETER;
1285 case MAC_MAX_CSMA_BACKOFFS:
1287 status = IEEE802154_INVALID_PARAMETER;
1289 case MAC_MAX_FRAME_RETRIES:
1291 status = IEEE802154_INVALID_PARAMETER;
1295 status = IEEE802154_INVALID_PARAMETER;
1297 case MAC_RESPONSE_WAIT_TIME:
1298 if (value < 2 || value > 64)
1299 status = IEEE802154_INVALID_PARAMETER;
1301 case MAC_SUPERFRAME_ORDER:
1303 status = IEEE802154_INVALID_PARAMETER;
1306 case MAC_ASSOCIATED_PAN_COORD:
1307 case MAC_ASSOCIATION_PERMIT:
1308 case MAC_AUTO_REQUEST:
1309 case MAC_BATT_LIFE_EXT:
1310 case MAC_GTS_PERMIT:
1311 case MAC_PROMISCUOUS_MODE:
1312 case MAC_RX_ON_WHEN_IDLE:
1313 case MAC_SECURITY_ENABLED:
1315 status = IEEE802154_INVALID_PARAMETER;
1318 case MAC_AUTO_REQUEST_SECURITY_LEVEL:
1320 status = IEEE802154_INVALID_PARAMETER;
1322 case MAC_AUTO_REQUEST_KEY_ID_MODE:
1324 status = IEEE802154_INVALID_PARAMETER;
1334 * tdme_settxpower() - Sets the tx power for MLME_SET phyTransmitPower
1335 * @txp: Transmit Power
1336 * @device_ref: Nondescript pointer to target device
1338 * Normalised to 802.15.4 Definition (6-bit, signed):
1340 * Bit 5-0: tx power (-32 - +31 dB)
1342 * Return: 802.15.4 status code of api calls
1344 static u8 tdme_settxpower(u8 txp, void *device_ref)
1349 union pa_cfg_sfr pa_cfg_val;
1351 /* extend from 6 to 8 bit */
1352 txp_ext = 0x3F & txp;
1355 txp_val = (s8)txp_ext;
1357 if (CA8210_MAC_MPW) {
1359 /* 8 dBm: ptrim = 5, itrim = +3 => +4 dBm */
1360 pa_cfg_val.bias_current_trim = 3;
1361 pa_cfg_val.buffer_capacitor_trim = 5;
1362 pa_cfg_val.boost = 1;
1364 /* 0 dBm: ptrim = 7, itrim = +3 => -6 dBm */
1365 pa_cfg_val.bias_current_trim = 3;
1366 pa_cfg_val.buffer_capacitor_trim = 7;
1367 pa_cfg_val.boost = 0;
1370 status = tdme_setsfr_request_sync(
1377 /* Look-Up Table for Setting Current and Frequency Trim values
1378 * for desired Output Power
1381 pa_cfg_val.paib = 0x3F;
1382 } else if (txp_val == 8) {
1383 pa_cfg_val.paib = 0x32;
1384 } else if (txp_val == 7) {
1385 pa_cfg_val.paib = 0x22;
1386 } else if (txp_val == 6) {
1387 pa_cfg_val.paib = 0x18;
1388 } else if (txp_val == 5) {
1389 pa_cfg_val.paib = 0x10;
1390 } else if (txp_val == 4) {
1391 pa_cfg_val.paib = 0x0C;
1392 } else if (txp_val == 3) {
1393 pa_cfg_val.paib = 0x08;
1394 } else if (txp_val == 2) {
1395 pa_cfg_val.paib = 0x05;
1396 } else if (txp_val == 1) {
1397 pa_cfg_val.paib = 0x03;
1398 } else if (txp_val == 0) {
1399 pa_cfg_val.paib = 0x01;
1401 pa_cfg_val.paib = 0x00;
1404 status = tdme_setsfr_request_sync(
1416 * mcps_data_request() - mcps_data_request (Send Data) according to API Spec
1417 * @src_addr_mode: Source Addressing Mode
1418 * @dst_address_mode: Destination Addressing Mode
1419 * @dst_pan_id: Destination PAN ID
1420 * @dst_addr: Pointer to Destination Address
1421 * @msdu_length: length of Data
1422 * @msdu: Pointer to Data
1423 * @msdu_handle: Handle of Data
1424 * @tx_options: Tx Options Bit Field
1425 * @security: Pointer to Security Structure or NULL
1426 * @device_ref: Nondescript pointer to target device
1428 * Return: 802.15.4 status code of action
1430 static u8 mcps_data_request(
1432 u8 dst_address_mode,
1434 union macaddr *dst_addr,
1439 struct secspec *security,
1443 struct secspec *psec;
1444 struct mac_message command;
1446 command.command_id = SPI_MCPS_DATA_REQUEST;
1447 command.pdata.data_req.src_addr_mode = src_addr_mode;
1448 command.pdata.data_req.dst.mode = dst_address_mode;
1449 if (dst_address_mode != MAC_MODE_NO_ADDR) {
1450 command.pdata.data_req.dst.pan_id[0] = LS_BYTE(dst_pan_id);
1451 command.pdata.data_req.dst.pan_id[1] = MS_BYTE(dst_pan_id);
1452 if (dst_address_mode == MAC_MODE_SHORT_ADDR) {
1453 command.pdata.data_req.dst.address[0] = LS_BYTE(
1454 dst_addr->short_address
1456 command.pdata.data_req.dst.address[1] = MS_BYTE(
1457 dst_addr->short_address
1459 } else { /* MAC_MODE_LONG_ADDR*/
1461 command.pdata.data_req.dst.address,
1462 dst_addr->ieee_address,
1467 command.pdata.data_req.msdu_length = msdu_length;
1468 command.pdata.data_req.msdu_handle = msdu_handle;
1469 command.pdata.data_req.tx_options = tx_options;
1470 memcpy(command.pdata.data_req.msdu, msdu, msdu_length);
1471 psec = (struct secspec *)(command.pdata.data_req.msdu + msdu_length);
1472 command.length = sizeof(struct mcps_data_request_pset) -
1473 MAX_DATA_SIZE + msdu_length;
1474 if (!security || security->security_level == 0) {
1475 psec->security_level = 0;
1476 command.length += 1;
1479 command.length += sizeof(struct secspec);
1482 if (ca8210_spi_transfer(device_ref, &command.command_id,
1483 command.length + 2))
1484 return IEEE802154_SYSTEM_ERROR;
1486 return IEEE802154_SUCCESS;
1490 * mlme_reset_request_sync() - MLME_RESET_request/confirm according to API Spec
1491 * @set_default_pib: Set defaults in PIB
1492 * @device_ref: Nondescript pointer to target device
1494 * Return: 802.15.4 status code of MLME-RESET.confirm
1496 static u8 mlme_reset_request_sync(
1502 struct mac_message command, response;
1503 struct spi_device *spi = device_ref;
1505 command.command_id = SPI_MLME_RESET_REQUEST;
1507 command.pdata.u8param = set_default_pib;
1509 if (cascoda_api_downstream(
1510 &command.command_id,
1512 &response.command_id,
1514 dev_err(&spi->dev, "cascoda_api_downstream failed\n");
1515 return IEEE802154_SYSTEM_ERROR;
1518 if (response.command_id != SPI_MLME_RESET_CONFIRM)
1519 return IEEE802154_SYSTEM_ERROR;
1521 status = response.pdata.status;
1523 /* reset COORD Bit for Channel Filtering as Coordinator */
1524 if (CA8210_MAC_WORKAROUNDS && set_default_pib && !status) {
1525 status = tdme_setsfr_request_sync(
1537 * mlme_set_request_sync() - MLME_SET_request/confirm according to API Spec
1538 * @pib_attribute: Attribute Number
1539 * @pib_attribute_index: Index within Attribute if an Array
1540 * @pib_attribute_length: Attribute length
1541 * @pib_attribute_value: Pointer to Attribute Value
1542 * @device_ref: Nondescript pointer to target device
1544 * Return: 802.15.4 status code of MLME-SET.confirm
1546 static u8 mlme_set_request_sync(
1548 u8 pib_attribute_index,
1549 u8 pib_attribute_length,
1550 const void *pib_attribute_value,
1555 struct mac_message command, response;
1557 /* pre-check the validity of pib_attribute values that are not checked
1560 if (tdme_checkpibattribute(
1561 pib_attribute, pib_attribute_length, pib_attribute_value)) {
1562 return IEEE802154_INVALID_PARAMETER;
1565 if (pib_attribute == PHY_CURRENT_CHANNEL) {
1566 status = tdme_channelinit(
1567 *((u8 *)pib_attribute_value),
1574 if (pib_attribute == PHY_TRANSMIT_POWER) {
1575 return tdme_settxpower(
1576 *((u8 *)pib_attribute_value),
1581 command.command_id = SPI_MLME_SET_REQUEST;
1582 command.length = sizeof(struct mlme_set_request_pset) -
1583 MAX_ATTRIBUTE_SIZE + pib_attribute_length;
1584 command.pdata.set_req.pib_attribute = pib_attribute;
1585 command.pdata.set_req.pib_attribute_index = pib_attribute_index;
1586 command.pdata.set_req.pib_attribute_length = pib_attribute_length;
1588 command.pdata.set_req.pib_attribute_value,
1589 pib_attribute_value,
1590 pib_attribute_length
1593 if (cascoda_api_downstream(
1594 &command.command_id,
1596 &response.command_id,
1598 return IEEE802154_SYSTEM_ERROR;
1601 if (response.command_id != SPI_MLME_SET_CONFIRM)
1602 return IEEE802154_SYSTEM_ERROR;
1604 return response.pdata.status;
1608 * hwme_set_request_sync() - HWME_SET_request/confirm according to API Spec
1609 * @hw_attribute: Attribute Number
1610 * @hw_attribute_length: Attribute length
1611 * @hw_attribute_value: Pointer to Attribute Value
1612 * @device_ref: Nondescript pointer to target device
1614 * Return: 802.15.4 status code of HWME-SET.confirm
1616 static u8 hwme_set_request_sync(
1618 u8 hw_attribute_length,
1619 u8 *hw_attribute_value,
1623 struct mac_message command, response;
1625 command.command_id = SPI_HWME_SET_REQUEST;
1626 command.length = 2 + hw_attribute_length;
1627 command.pdata.hwme_set_req.hw_attribute = hw_attribute;
1628 command.pdata.hwme_set_req.hw_attribute_length = hw_attribute_length;
1630 command.pdata.hwme_set_req.hw_attribute_value,
1635 if (cascoda_api_downstream(
1636 &command.command_id,
1638 &response.command_id,
1640 return IEEE802154_SYSTEM_ERROR;
1643 if (response.command_id != SPI_HWME_SET_CONFIRM)
1644 return IEEE802154_SYSTEM_ERROR;
1646 return response.pdata.hwme_set_cnf.status;
1650 * hwme_get_request_sync() - HWME_GET_request/confirm according to API Spec
1651 * @hw_attribute: Attribute Number
1652 * @hw_attribute_length: Attribute length
1653 * @hw_attribute_value: Pointer to Attribute Value
1654 * @device_ref: Nondescript pointer to target device
1656 * Return: 802.15.4 status code of HWME-GET.confirm
1658 static u8 hwme_get_request_sync(
1660 u8 *hw_attribute_length,
1661 u8 *hw_attribute_value,
1665 struct mac_message command, response;
1667 command.command_id = SPI_HWME_GET_REQUEST;
1669 command.pdata.hwme_get_req.hw_attribute = hw_attribute;
1671 if (cascoda_api_downstream(
1672 &command.command_id,
1674 &response.command_id,
1676 return IEEE802154_SYSTEM_ERROR;
1679 if (response.command_id != SPI_HWME_GET_CONFIRM)
1680 return IEEE802154_SYSTEM_ERROR;
1682 if (response.pdata.hwme_get_cnf.status == IEEE802154_SUCCESS) {
1683 *hw_attribute_length =
1684 response.pdata.hwme_get_cnf.hw_attribute_length;
1687 response.pdata.hwme_get_cnf.hw_attribute_value,
1688 *hw_attribute_length
1692 return response.pdata.hwme_get_cnf.status;
1695 /* Network driver operation */
1698 * ca8210_async_xmit_complete() - Called to announce that an asynchronous
1699 * transmission has finished
1700 * @hw: ieee802154_hw of ca8210 that has finished exchange
1701 * @msduhandle: Identifier of transmission that has completed
1702 * @status: Returned 802.15.4 status code of the transmission
1704 * Return: 0 or linux error code
1706 static int ca8210_async_xmit_complete(
1707 struct ieee802154_hw *hw,
1711 struct ca8210_priv *priv = hw->priv;
1713 if (priv->nextmsduhandle != msduhandle) {
1716 "Unexpected msdu_handle on data confirm, Expected %d, got %d\n",
1717 priv->nextmsduhandle,
1723 priv->async_tx_pending = false;
1724 priv->nextmsduhandle++;
1729 "Link transmission unsuccessful, status = %d\n",
1732 if (status != IEEE802154_TRANSACTION_OVERFLOW) {
1733 ieee802154_xmit_error(priv->hw, priv->tx_skb, status);
1737 ieee802154_xmit_complete(priv->hw, priv->tx_skb, true);
1743 * ca8210_skb_rx() - Contructs a properly framed socket buffer from a received
1744 * MCPS_DATA_indication
1745 * @hw: ieee802154_hw that MCPS_DATA_indication was received by
1746 * @len: length of MCPS_DATA_indication
1747 * @data_ind: Octet array of MCPS_DATA_indication
1749 * Called by the spi driver whenever a SAP command is received, this function
1750 * will ascertain whether the command is of interest to the network driver and
1751 * take necessary action.
1753 * Return: 0 or linux error code
1755 static int ca8210_skb_rx(
1756 struct ieee802154_hw *hw,
1761 struct ieee802154_hdr hdr;
1764 u8 mpdulinkquality = data_ind[23];
1765 struct sk_buff *skb;
1766 struct ca8210_priv *priv = hw->priv;
1768 /* Allocate mtu size buffer for every rx packet */
1769 skb = dev_alloc_skb(IEEE802154_MTU + sizeof(hdr));
1773 skb_reserve(skb, sizeof(hdr));
1775 msdulen = data_ind[22]; /* msdu_length */
1776 if (msdulen > IEEE802154_MTU) {
1779 "received erroneously large msdu length!\n"
1784 dev_dbg(&priv->spi->dev, "skb buffer length = %d\n", msdulen);
1786 if (priv->promiscuous)
1790 hdr.sec.level = data_ind[29 + msdulen];
1791 dev_dbg(&priv->spi->dev, "security level: %#03x\n", hdr.sec.level);
1792 if (hdr.sec.level > 0) {
1793 hdr.sec.key_id_mode = data_ind[30 + msdulen];
1794 memcpy(&hdr.sec.extended_src, &data_ind[31 + msdulen], 8);
1795 hdr.sec.key_id = data_ind[39 + msdulen];
1797 hdr.source.mode = data_ind[0];
1798 dev_dbg(&priv->spi->dev, "srcAddrMode: %#03x\n", hdr.source.mode);
1799 hdr.source.pan_id = *(u16 *)&data_ind[1];
1800 dev_dbg(&priv->spi->dev, "srcPanId: %#06x\n", hdr.source.pan_id);
1801 memcpy(&hdr.source.extended_addr, &data_ind[3], 8);
1802 hdr.dest.mode = data_ind[11];
1803 dev_dbg(&priv->spi->dev, "dstAddrMode: %#03x\n", hdr.dest.mode);
1804 hdr.dest.pan_id = *(u16 *)&data_ind[12];
1805 dev_dbg(&priv->spi->dev, "dstPanId: %#06x\n", hdr.dest.pan_id);
1806 memcpy(&hdr.dest.extended_addr, &data_ind[14], 8);
1808 /* Fill in FC implicitly */
1809 hdr.fc.type = 1; /* Data frame */
1811 hdr.fc.security_enabled = 1;
1813 hdr.fc.security_enabled = 0;
1814 if (data_ind[1] != data_ind[12] || data_ind[2] != data_ind[13])
1815 hdr.fc.intra_pan = 1;
1817 hdr.fc.intra_pan = 0;
1818 hdr.fc.dest_addr_mode = hdr.dest.mode;
1819 hdr.fc.source_addr_mode = hdr.source.mode;
1821 /* Add hdr to front of buffer */
1822 hlen = ieee802154_hdr_push(skb, &hdr);
1825 dev_crit(&priv->spi->dev, "failed to push mac hdr onto skb!\n");
1830 skb_reset_mac_header(skb);
1831 skb->mac_len = hlen;
1834 /* Add <msdulen> bytes of space to the back of the buffer */
1835 /* Copy msdu to skb */
1836 skb_put_data(skb, &data_ind[29], msdulen);
1838 ieee802154_rx_irqsafe(hw, skb, mpdulinkquality);
1843 * ca8210_net_rx() - Acts upon received SAP commands relevant to the network
1845 * @hw: ieee802154_hw that command was received by
1846 * @command: Octet array of received command
1847 * @len: length of the received command
1849 * Called by the spi driver whenever a SAP command is received, this function
1850 * will ascertain whether the command is of interest to the network driver and
1851 * take necessary action.
1853 * Return: 0 or linux error code
1855 static int ca8210_net_rx(struct ieee802154_hw *hw, u8 *command, size_t len)
1857 struct ca8210_priv *priv = hw->priv;
1858 unsigned long flags;
1861 dev_dbg(&priv->spi->dev, "%s: CmdID = %d\n", __func__, command[0]);
1863 if (command[0] == SPI_MCPS_DATA_INDICATION) {
1865 spin_lock_irqsave(&priv->lock, flags);
1866 if (command[26] == priv->last_dsn) {
1869 "DSN %d resend received, ignoring...\n",
1872 spin_unlock_irqrestore(&priv->lock, flags);
1875 priv->last_dsn = command[26];
1876 spin_unlock_irqrestore(&priv->lock, flags);
1877 return ca8210_skb_rx(hw, len - 2, command + 2);
1878 } else if (command[0] == SPI_MCPS_DATA_CONFIRM) {
1879 status = command[3];
1880 if (priv->async_tx_pending) {
1881 return ca8210_async_xmit_complete(
1893 * ca8210_skb_tx() - Transmits a given socket buffer using the ca8210
1894 * @skb: Socket buffer to transmit
1895 * @msduhandle: Data identifier to pass to the 802.15.4 MAC
1896 * @priv: Pointer to private data section of target ca8210
1898 * Return: 0 or linux error code
1900 static int ca8210_skb_tx(
1901 struct sk_buff *skb,
1903 struct ca8210_priv *priv
1906 struct ieee802154_hdr header = { };
1907 struct secspec secspec;
1908 int mac_len, status;
1910 dev_dbg(&priv->spi->dev, "%s called\n", __func__);
1912 /* Get addressing info from skb - ieee802154 layer creates a full
1915 mac_len = ieee802154_hdr_peek_addrs(skb, &header);
1919 secspec.security_level = header.sec.level;
1920 secspec.key_id_mode = header.sec.key_id_mode;
1921 if (secspec.key_id_mode == 2)
1922 memcpy(secspec.key_source, &header.sec.short_src, 4);
1923 else if (secspec.key_id_mode == 3)
1924 memcpy(secspec.key_source, &header.sec.extended_src, 8);
1925 secspec.key_index = header.sec.key_id;
1927 /* Pass to Cascoda API */
1928 status = mcps_data_request(
1932 (union macaddr *)&header.dest.extended_addr,
1934 &skb->data[mac_len],
1936 header.fc.ack_request,
1940 return link_to_linux_err(status);
1944 * ca8210_start() - Starts the network driver
1945 * @hw: ieee802154_hw of ca8210 being started
1947 * Return: 0 or linux error code
1949 static int ca8210_start(struct ieee802154_hw *hw)
1953 u8 lqi_threshold = 0;
1954 struct ca8210_priv *priv = hw->priv;
1956 priv->last_dsn = -1;
1957 /* Turn receiver on when idle for now just to test rx */
1958 rx_on_when_idle = 1;
1959 status = mlme_set_request_sync(
1960 MAC_RX_ON_WHEN_IDLE,
1969 "Setting rx_on_when_idle failed, status = %d\n",
1972 return link_to_linux_err(status);
1974 status = hwme_set_request_sync(
1983 "Setting lqilimit failed, status = %d\n",
1986 return link_to_linux_err(status);
1993 * ca8210_stop() - Stops the network driver
1994 * @hw: ieee802154_hw of ca8210 being stopped
1996 * Return: 0 or linux error code
1998 static void ca8210_stop(struct ieee802154_hw *hw)
2003 * ca8210_xmit_async() - Asynchronously transmits a given socket buffer using
2005 * @hw: ieee802154_hw of ca8210 to transmit from
2006 * @skb: Socket buffer to transmit
2008 * Return: 0 or linux error code
2010 static int ca8210_xmit_async(struct ieee802154_hw *hw, struct sk_buff *skb)
2012 struct ca8210_priv *priv = hw->priv;
2015 dev_dbg(&priv->spi->dev, "calling %s\n", __func__);
2018 priv->async_tx_pending = true;
2019 status = ca8210_skb_tx(skb, priv->nextmsduhandle, priv);
2024 * ca8210_get_ed() - Returns the measured energy on the current channel at this
2026 * @hw: ieee802154_hw of target ca8210
2027 * @level: Measured Energy Detect level
2029 * Return: 0 or linux error code
2031 static int ca8210_get_ed(struct ieee802154_hw *hw, u8 *level)
2034 struct ca8210_priv *priv = hw->priv;
2036 return link_to_linux_err(
2037 hwme_get_request_sync(HWME_EDVALUE, &lenvar, level, priv->spi)
2042 * ca8210_set_channel() - Sets the current operating 802.15.4 channel of the
2044 * @hw: ieee802154_hw of target ca8210
2045 * @page: Channel page to set
2046 * @channel: Channel number to set
2048 * Return: 0 or linux error code
2050 static int ca8210_set_channel(
2051 struct ieee802154_hw *hw,
2057 struct ca8210_priv *priv = hw->priv;
2059 status = mlme_set_request_sync(
2060 PHY_CURRENT_CHANNEL,
2069 "error setting channel, MLME-SET.confirm status = %d\n",
2073 return link_to_linux_err(status);
2077 * ca8210_set_hw_addr_filt() - Sets the address filtering parameters of the
2079 * @hw: ieee802154_hw of target ca8210
2080 * @filt: Filtering parameters
2081 * @changed: Bitmap representing which parameters to change
2083 * Effectively just sets the actual addressing information identifying this node
2084 * as all filtering is performed by the ca8210 as detailed in the IEEE 802.15.4
2085 * 2006 specification.
2087 * Return: 0 or linux error code
2089 static int ca8210_set_hw_addr_filt(
2090 struct ieee802154_hw *hw,
2091 struct ieee802154_hw_addr_filt *filt,
2092 unsigned long changed
2096 struct ca8210_priv *priv = hw->priv;
2098 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
2099 status = mlme_set_request_sync(
2103 &filt->pan_id, priv->spi
2108 "error setting pan id, MLME-SET.confirm status = %d",
2111 return link_to_linux_err(status);
2114 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
2115 status = mlme_set_request_sync(
2119 &filt->short_addr, priv->spi
2124 "error setting short address, MLME-SET.confirm status = %d",
2127 return link_to_linux_err(status);
2130 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
2131 status = mlme_set_request_sync(
2141 "error setting ieee address, MLME-SET.confirm status = %d",
2144 return link_to_linux_err(status);
2147 /* TODO: Should use MLME_START to set coord bit? */
2152 * ca8210_set_tx_power() - Sets the transmit power of the ca8210
2153 * @hw: ieee802154_hw of target ca8210
2154 * @mbm: Transmit power in mBm (dBm*100)
2156 * Return: 0 or linux error code
2158 static int ca8210_set_tx_power(struct ieee802154_hw *hw, s32 mbm)
2160 struct ca8210_priv *priv = hw->priv;
2163 return link_to_linux_err(
2164 mlme_set_request_sync(PHY_TRANSMIT_POWER, 0, 1, &mbm, priv->spi)
2169 * ca8210_set_cca_mode() - Sets the clear channel assessment mode of the ca8210
2170 * @hw: ieee802154_hw of target ca8210
2171 * @cca: CCA mode to set
2173 * Return: 0 or linux error code
2175 static int ca8210_set_cca_mode(
2176 struct ieee802154_hw *hw,
2177 const struct wpan_phy_cca *cca
2182 struct ca8210_priv *priv = hw->priv;
2184 cca_mode = cca->mode & 3;
2185 if (cca_mode == 3 && cca->opt == NL802154_CCA_OPT_ENERGY_CARRIER_OR) {
2186 /* cca_mode 0 == CS OR ED, 3 == CS AND ED */
2189 status = mlme_set_request_sync(
2199 "error setting cca mode, MLME-SET.confirm status = %d",
2203 return link_to_linux_err(status);
2207 * ca8210_set_cca_ed_level() - Sets the CCA ED level of the ca8210
2208 * @hw: ieee802154_hw of target ca8210
2209 * @level: ED level to set (in mbm)
2211 * Sets the minimum threshold of measured energy above which the ca8210 will
2212 * back off and retry a transmission.
2214 * Return: 0 or linux error code
2216 static int ca8210_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
2219 u8 ed_threshold = (level / 100) * 2 + 256;
2220 struct ca8210_priv *priv = hw->priv;
2222 status = hwme_set_request_sync(
2231 "error setting ed threshold, HWME-SET.confirm status = %d",
2235 return link_to_linux_err(status);
2239 * ca8210_set_csma_params() - Sets the CSMA parameters of the ca8210
2240 * @hw: ieee802154_hw of target ca8210
2241 * @min_be: Minimum backoff exponent when backing off a transmission
2242 * @max_be: Maximum backoff exponent when backing off a transmission
2243 * @retries: Number of times to retry after backing off
2245 * Return: 0 or linux error code
2247 static int ca8210_set_csma_params(
2248 struct ieee802154_hw *hw,
2255 struct ca8210_priv *priv = hw->priv;
2257 status = mlme_set_request_sync(MAC_MIN_BE, 0, 1, &min_be, priv->spi);
2261 "error setting min be, MLME-SET.confirm status = %d",
2264 return link_to_linux_err(status);
2266 status = mlme_set_request_sync(MAC_MAX_BE, 0, 1, &max_be, priv->spi);
2270 "error setting max be, MLME-SET.confirm status = %d",
2273 return link_to_linux_err(status);
2275 status = mlme_set_request_sync(
2276 MAC_MAX_CSMA_BACKOFFS,
2285 "error setting max csma backoffs, MLME-SET.confirm status = %d",
2289 return link_to_linux_err(status);
2293 * ca8210_set_frame_retries() - Sets the maximum frame retries of the ca8210
2294 * @hw: ieee802154_hw of target ca8210
2295 * @retries: Number of retries
2297 * Sets the number of times to retry a transmission if no acknowledgment was
2298 * received from the other end when one was requested.
2300 * Return: 0 or linux error code
2302 static int ca8210_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
2305 struct ca8210_priv *priv = hw->priv;
2307 status = mlme_set_request_sync(
2308 MAC_MAX_FRAME_RETRIES,
2317 "error setting frame retries, MLME-SET.confirm status = %d",
2321 return link_to_linux_err(status);
2324 static int ca8210_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
2327 struct ca8210_priv *priv = hw->priv;
2329 status = mlme_set_request_sync(
2330 MAC_PROMISCUOUS_MODE,
2339 "error setting promiscuous mode, MLME-SET.confirm status = %d",
2343 priv->promiscuous = on;
2345 return link_to_linux_err(status);
2348 static const struct ieee802154_ops ca8210_phy_ops = {
2349 .start = ca8210_start,
2350 .stop = ca8210_stop,
2351 .xmit_async = ca8210_xmit_async,
2352 .ed = ca8210_get_ed,
2353 .set_channel = ca8210_set_channel,
2354 .set_hw_addr_filt = ca8210_set_hw_addr_filt,
2355 .set_txpower = ca8210_set_tx_power,
2356 .set_cca_mode = ca8210_set_cca_mode,
2357 .set_cca_ed_level = ca8210_set_cca_ed_level,
2358 .set_csma_params = ca8210_set_csma_params,
2359 .set_frame_retries = ca8210_set_frame_retries,
2360 .set_promiscuous_mode = ca8210_set_promiscuous_mode
2363 /* Test/EVBME Interface */
2366 * ca8210_test_int_open() - Opens the test interface to the userspace
2367 * @inodp: inode representation of file interface
2368 * @filp: file interface
2370 * Return: 0 or linux error code
2372 static int ca8210_test_int_open(struct inode *inodp, struct file *filp)
2374 struct ca8210_priv *priv = inodp->i_private;
2376 filp->private_data = priv;
2381 * ca8210_test_check_upstream() - Checks a command received from the upstream
2382 * testing interface for required action
2383 * @buf: Buffer containing command to check
2384 * @device_ref: Nondescript pointer to target device
2386 * Return: 0 or linux error code
2388 static int ca8210_test_check_upstream(u8 *buf, void *device_ref)
2391 u8 response[CA8210_SPI_BUF_SIZE];
2393 if (buf[0] == SPI_MLME_SET_REQUEST) {
2394 ret = tdme_checkpibattribute(buf[2], buf[4], buf + 5);
2396 response[0] = SPI_MLME_SET_CONFIRM;
2398 response[2] = IEEE802154_INVALID_PARAMETER;
2399 response[3] = buf[2];
2400 response[4] = buf[3];
2401 if (cascoda_api_upstream)
2402 cascoda_api_upstream(response, 5, device_ref);
2406 if (buf[0] == SPI_MLME_ASSOCIATE_REQUEST) {
2407 return tdme_channelinit(buf[2], device_ref);
2408 } else if (buf[0] == SPI_MLME_START_REQUEST) {
2409 return tdme_channelinit(buf[4], device_ref);
2411 (buf[0] == SPI_MLME_SET_REQUEST) &&
2412 (buf[2] == PHY_CURRENT_CHANNEL)
2414 return tdme_channelinit(buf[5], device_ref);
2416 (buf[0] == SPI_TDME_SET_REQUEST) &&
2417 (buf[2] == TDME_CHANNEL)
2419 return tdme_channelinit(buf[4], device_ref);
2421 (CA8210_MAC_WORKAROUNDS) &&
2422 (buf[0] == SPI_MLME_RESET_REQUEST) &&
2425 /* reset COORD Bit for Channel Filtering as Coordinator */
2426 return tdme_setsfr_request_sync(
2434 } /* End of EVBMECheckSerialCommand() */
2437 * ca8210_test_int_user_write() - Called by a process in userspace to send a
2438 * message to the ca8210 drivers
2439 * @filp: file interface
2440 * @in_buf: Buffer containing message to write
2441 * @len: length of message
2444 * Return: 0 or linux error code
2446 static ssize_t ca8210_test_int_user_write(
2448 const char __user *in_buf,
2454 struct ca8210_priv *priv = filp->private_data;
2455 u8 command[CA8210_SPI_BUF_SIZE];
2457 memset(command, SPI_IDLE, 6);
2458 if (len > CA8210_SPI_BUF_SIZE || len < 2) {
2461 "userspace requested erroneous write length (%zu)\n",
2467 ret = copy_from_user(command, in_buf, len);
2471 "%d bytes could not be copied from userspace\n",
2476 if (len != command[1] + 2) {
2479 "write len does not match packet length field\n"
2484 ret = ca8210_test_check_upstream(command, priv->spi);
2486 ret = ca8210_spi_exchange(
2493 /* effectively 0 bytes were written successfully */
2496 "spi exchange failed\n"
2500 if (command[0] & SPI_SYN)
2508 * ca8210_test_int_user_read() - Called by a process in userspace to read a
2509 * message from the ca8210 drivers
2510 * @filp: file interface
2511 * @buf: Buffer to write message to
2512 * @len: length of message to read (ignored)
2513 * @offp: file offset
2515 * If the O_NONBLOCK flag was set when opening the file then this function will
2516 * not block, i.e. it will return if the fifo is empty. Otherwise the function
2517 * will block, i.e. wait until new data arrives.
2519 * Return: number of bytes read
2521 static ssize_t ca8210_test_int_user_read(
2529 struct ca8210_priv *priv = filp->private_data;
2530 unsigned char *fifo_buffer;
2531 unsigned long bytes_not_copied;
2533 if (filp->f_flags & O_NONBLOCK) {
2534 /* Non-blocking mode */
2535 if (kfifo_is_empty(&priv->test.up_fifo))
2539 wait_event_interruptible(
2541 !kfifo_is_empty(&priv->test.up_fifo)
2545 if (kfifo_out(&priv->test.up_fifo, &fifo_buffer, 4) != 4) {
2548 "test_interface: Wrong number of elements popped from upstream fifo\n"
2552 cmdlen = fifo_buffer[1];
2553 bytes_not_copied = cmdlen + 2;
2555 bytes_not_copied = copy_to_user(buf, fifo_buffer, bytes_not_copied);
2556 if (bytes_not_copied > 0) {
2559 "%lu bytes could not be copied to user space!\n",
2564 dev_dbg(&priv->spi->dev, "test_interface: Cmd len = %d\n", cmdlen);
2566 dev_dbg(&priv->spi->dev, "test_interface: Read\n");
2567 for (i = 0; i < cmdlen + 2; i++)
2568 dev_dbg(&priv->spi->dev, "%#03x\n", fifo_buffer[i]);
2576 * ca8210_test_int_ioctl() - Called by a process in userspace to enact an
2578 * @filp: file interface
2579 * @ioctl_num: which action to enact
2580 * @ioctl_param: arbitrary parameter for the action
2584 static long ca8210_test_int_ioctl(
2586 unsigned int ioctl_num,
2587 unsigned long ioctl_param
2590 struct ca8210_priv *priv = filp->private_data;
2592 switch (ioctl_num) {
2593 case CA8210_IOCTL_HARD_RESET:
2594 ca8210_reset_send(priv->spi, ioctl_param);
2603 * ca8210_test_int_poll() - Called by a process in userspace to determine which
2604 * actions are currently possible for the file
2605 * @filp: file interface
2606 * @ptable: poll table
2608 * Return: set of poll return flags
2610 static __poll_t ca8210_test_int_poll(
2612 struct poll_table_struct *ptable
2615 __poll_t return_flags = 0;
2616 struct ca8210_priv *priv = filp->private_data;
2618 poll_wait(filp, &priv->test.readq, ptable);
2619 if (!kfifo_is_empty(&priv->test.up_fifo))
2620 return_flags |= (EPOLLIN | EPOLLRDNORM);
2621 if (wait_event_interruptible(
2623 !kfifo_is_empty(&priv->test.up_fifo))) {
2626 return return_flags;
2629 static const struct file_operations test_int_fops = {
2630 .read = ca8210_test_int_user_read,
2631 .write = ca8210_test_int_user_write,
2632 .open = ca8210_test_int_open,
2634 .unlocked_ioctl = ca8210_test_int_ioctl,
2635 .poll = ca8210_test_int_poll
2641 * ca8210_get_platform_data() - Populate a ca8210_platform_data object
2642 * @spi_device: Pointer to ca8210 spi device object to get data for
2643 * @pdata: Pointer to ca8210_platform_data object to populate
2645 * Return: 0 or linux error code
2647 static int ca8210_get_platform_data(
2648 struct spi_device *spi_device,
2649 struct ca8210_platform_data *pdata
2654 if (!spi_device->dev.of_node)
2657 pdata->extclockenable = of_property_read_bool(
2658 spi_device->dev.of_node,
2661 if (pdata->extclockenable) {
2662 ret = of_property_read_u32(
2663 spi_device->dev.of_node,
2665 &pdata->extclockfreq
2670 ret = of_property_read_u32(
2671 spi_device->dev.of_node,
2673 &pdata->extclockgpio
2681 * ca8210_config_extern_clk() - Configure the external clock provided by the
2683 * @pdata: Pointer to ca8210_platform_data containing clock parameters
2684 * @spi: Pointer to target ca8210 spi device
2685 * @on: True to turn the clock on, false to turn off
2687 * The external clock is configured with a frequency and output pin taken from
2688 * the platform data.
2690 * Return: 0 or linux error code
2692 static int ca8210_config_extern_clk(
2693 struct ca8210_platform_data *pdata,
2694 struct spi_device *spi,
2701 dev_info(&spi->dev, "Switching external clock on\n");
2702 switch (pdata->extclockfreq) {
2719 dev_crit(&spi->dev, "Invalid extclock-freq\n");
2722 clkparam[1] = pdata->extclockgpio;
2724 dev_info(&spi->dev, "Switching external clock off\n");
2725 clkparam[0] = 0; /* off */
2728 return link_to_linux_err(
2729 hwme_set_request_sync(HWME_SYSCLKOUT, 2, clkparam, spi)
2734 * ca8210_register_ext_clock() - Register ca8210's external clock with kernel
2735 * @spi: Pointer to target ca8210 spi device
2737 * Return: 0 or linux error code
2739 static int ca8210_register_ext_clock(struct spi_device *spi)
2741 struct device_node *np = spi->dev.of_node;
2742 struct ca8210_priv *priv = spi_get_drvdata(spi);
2743 struct ca8210_platform_data *pdata = spi->dev.platform_data;
2749 priv->clk = clk_register_fixed_rate(
2757 if (IS_ERR(priv->clk)) {
2758 dev_crit(&spi->dev, "Failed to register external clk\n");
2759 return PTR_ERR(priv->clk);
2761 ret = of_clk_add_provider(np, of_clk_src_simple_get, priv->clk);
2763 clk_unregister(priv->clk);
2766 "Failed to register external clock as clock provider\n"
2769 dev_info(&spi->dev, "External clock set as clock provider\n");
2776 * ca8210_unregister_ext_clock() - Unregister ca8210's external clock with
2778 * @spi: Pointer to target ca8210 spi device
2780 static void ca8210_unregister_ext_clock(struct spi_device *spi)
2782 struct ca8210_priv *priv = spi_get_drvdata(spi);
2787 of_clk_del_provider(spi->dev.of_node);
2788 clk_unregister(priv->clk);
2789 dev_info(&spi->dev, "External clock unregistered\n");
2793 * ca8210_reset_init() - Initialise the reset input to the ca8210
2794 * @spi: Pointer to target ca8210 spi device
2796 * Return: 0 or linux error code
2798 static int ca8210_reset_init(struct spi_device *spi)
2801 struct ca8210_platform_data *pdata = spi->dev.platform_data;
2803 pdata->gpio_reset = of_get_named_gpio(
2809 ret = gpio_direction_output(pdata->gpio_reset, 1);
2813 "Reset GPIO %d did not set to output mode\n",
2822 * ca8210_interrupt_init() - Initialise the irq output from the ca8210
2823 * @spi: Pointer to target ca8210 spi device
2825 * Return: 0 or linux error code
2827 static int ca8210_interrupt_init(struct spi_device *spi)
2830 struct ca8210_platform_data *pdata = spi->dev.platform_data;
2832 pdata->gpio_irq = of_get_named_gpio(
2838 pdata->irq_id = gpio_to_irq(pdata->gpio_irq);
2839 if (pdata->irq_id < 0) {
2842 "Could not get irq for gpio pin %d\n",
2845 gpio_free(pdata->gpio_irq);
2846 return pdata->irq_id;
2851 ca8210_interrupt_handler,
2852 IRQF_TRIGGER_FALLING,
2854 spi_get_drvdata(spi)
2857 dev_crit(&spi->dev, "request_irq %d failed\n", pdata->irq_id);
2858 gpiod_unexport(gpio_to_desc(pdata->gpio_irq));
2859 gpio_free(pdata->gpio_irq);
2866 * ca8210_dev_com_init() - Initialise the spi communication component
2867 * @priv: Pointer to private data structure
2869 * Return: 0 or linux error code
2871 static int ca8210_dev_com_init(struct ca8210_priv *priv)
2873 priv->mlme_workqueue = alloc_ordered_workqueue(
2877 if (!priv->mlme_workqueue) {
2878 dev_crit(&priv->spi->dev, "alloc of mlme_workqueue failed!\n");
2882 priv->irq_workqueue = alloc_ordered_workqueue(
2883 "ca8210 irq worker",
2886 if (!priv->irq_workqueue) {
2887 dev_crit(&priv->spi->dev, "alloc of irq_workqueue failed!\n");
2888 destroy_workqueue(priv->mlme_workqueue);
2896 * ca8210_dev_com_clear() - Deinitialise the spi communication component
2897 * @priv: Pointer to private data structure
2899 static void ca8210_dev_com_clear(struct ca8210_priv *priv)
2901 destroy_workqueue(priv->mlme_workqueue);
2902 destroy_workqueue(priv->irq_workqueue);
2905 #define CA8210_MAX_TX_POWERS (9)
2906 static const s32 ca8210_tx_powers[CA8210_MAX_TX_POWERS] = {
2907 800, 700, 600, 500, 400, 300, 200, 100, 0
2910 #define CA8210_MAX_ED_LEVELS (21)
2911 static const s32 ca8210_ed_levels[CA8210_MAX_ED_LEVELS] = {
2912 -10300, -10250, -10200, -10150, -10100, -10050, -10000, -9950, -9900,
2913 -9850, -9800, -9750, -9700, -9650, -9600, -9550, -9500, -9450, -9400,
2918 * ca8210_hw_setup() - Populate the ieee802154_hw phy attributes with the
2920 * @ca8210_hw: Pointer to ieee802154_hw to populate
2922 static void ca8210_hw_setup(struct ieee802154_hw *ca8210_hw)
2924 /* Support channels 11-26 */
2925 ca8210_hw->phy->supported.channels[0] = CA8210_VALID_CHANNELS;
2926 ca8210_hw->phy->supported.tx_powers_size = CA8210_MAX_TX_POWERS;
2927 ca8210_hw->phy->supported.tx_powers = ca8210_tx_powers;
2928 ca8210_hw->phy->supported.cca_ed_levels_size = CA8210_MAX_ED_LEVELS;
2929 ca8210_hw->phy->supported.cca_ed_levels = ca8210_ed_levels;
2930 ca8210_hw->phy->current_channel = 18;
2931 ca8210_hw->phy->current_page = 0;
2932 ca8210_hw->phy->transmit_power = 800;
2933 ca8210_hw->phy->cca.mode = NL802154_CCA_ENERGY_CARRIER;
2934 ca8210_hw->phy->cca.opt = NL802154_CCA_OPT_ENERGY_CARRIER_AND;
2935 ca8210_hw->phy->cca_ed_level = -9800;
2936 ca8210_hw->phy->symbol_duration = 16;
2937 ca8210_hw->phy->lifs_period = 40 * ca8210_hw->phy->symbol_duration;
2938 ca8210_hw->phy->sifs_period = 12 * ca8210_hw->phy->symbol_duration;
2940 IEEE802154_HW_AFILT |
2941 IEEE802154_HW_OMIT_CKSUM |
2942 IEEE802154_HW_FRAME_RETRIES |
2943 IEEE802154_HW_PROMISCUOUS |
2944 IEEE802154_HW_CSMA_PARAMS;
2945 ca8210_hw->phy->flags =
2946 WPAN_PHY_FLAG_TXPOWER |
2947 WPAN_PHY_FLAG_CCA_ED_LEVEL |
2948 WPAN_PHY_FLAG_CCA_MODE;
2952 * ca8210_test_interface_init() - Initialise the test file interface
2953 * @priv: Pointer to private data structure
2955 * Provided as an alternative to the standard linux network interface, the test
2956 * interface exposes a file in the filesystem (ca8210_test) that allows
2957 * 802.15.4 SAP Commands and Cascoda EVBME commands to be sent directly to
2960 * Return: 0 or linux error code
2962 static int ca8210_test_interface_init(struct ca8210_priv *priv)
2964 struct ca8210_test *test = &priv->test;
2971 priv->spi->master->bus_num,
2972 priv->spi->chip_select
2975 test->ca8210_dfs_spi_int = debugfs_create_file(
2977 0600, /* S_IRUSR | S_IWUSR */
2983 debugfs_create_symlink("ca8210", NULL, node_name);
2984 init_waitqueue_head(&test->readq);
2987 CA8210_TEST_INT_FIFO_SIZE,
2993 * ca8210_test_interface_clear() - Deinitialise the test file interface
2994 * @priv: Pointer to private data structure
2996 static void ca8210_test_interface_clear(struct ca8210_priv *priv)
2998 struct ca8210_test *test = &priv->test;
3000 debugfs_remove(test->ca8210_dfs_spi_int);
3001 kfifo_free(&test->up_fifo);
3002 dev_info(&priv->spi->dev, "Test interface removed\n");
3006 * ca8210_remove() - Shut down a ca8210 upon being disconnected
3007 * @spi_device: Pointer to spi device data structure
3009 * Return: 0 or linux error code
3011 static void ca8210_remove(struct spi_device *spi_device)
3013 struct ca8210_priv *priv;
3014 struct ca8210_platform_data *pdata;
3016 dev_info(&spi_device->dev, "Removing ca8210\n");
3018 pdata = spi_device->dev.platform_data;
3020 if (pdata->extclockenable) {
3021 ca8210_unregister_ext_clock(spi_device);
3022 ca8210_config_extern_clk(pdata, spi_device, 0);
3024 free_irq(pdata->irq_id, spi_device->dev.driver_data);
3026 spi_device->dev.platform_data = NULL;
3028 /* get spi_device private data */
3029 priv = spi_get_drvdata(spi_device);
3033 "sync_down = %d, sync_up = %d\n",
3037 ca8210_dev_com_clear(spi_device->dev.driver_data);
3039 if (priv->hw_registered)
3040 ieee802154_unregister_hw(priv->hw);
3041 ieee802154_free_hw(priv->hw);
3045 "Unregistered & freed ieee802154_hw.\n"
3048 if (IS_ENABLED(CONFIG_IEEE802154_CA8210_DEBUGFS))
3049 ca8210_test_interface_clear(priv);
3054 * ca8210_probe() - Set up a connected ca8210 upon being detected by the system
3055 * @spi_device: Pointer to spi device data structure
3057 * Return: 0 or linux error code
3059 static int ca8210_probe(struct spi_device *spi_device)
3061 struct ca8210_priv *priv;
3062 struct ieee802154_hw *hw;
3063 struct ca8210_platform_data *pdata;
3066 dev_info(&spi_device->dev, "Inserting ca8210\n");
3068 /* allocate ieee802154_hw and private data */
3069 hw = ieee802154_alloc_hw(sizeof(struct ca8210_priv), &ca8210_phy_ops);
3071 dev_crit(&spi_device->dev, "ieee802154_alloc_hw failed\n");
3078 priv->spi = spi_device;
3079 hw->parent = &spi_device->dev;
3080 spin_lock_init(&priv->lock);
3081 priv->async_tx_pending = false;
3082 priv->hw_registered = false;
3084 priv->sync_down = 0;
3085 priv->promiscuous = false;
3087 init_completion(&priv->ca8210_is_awake);
3088 init_completion(&priv->spi_transfer_complete);
3089 init_completion(&priv->sync_exchange_complete);
3090 spi_set_drvdata(priv->spi, priv);
3091 if (IS_ENABLED(CONFIG_IEEE802154_CA8210_DEBUGFS)) {
3092 cascoda_api_upstream = ca8210_test_int_driver_write;
3093 ca8210_test_interface_init(priv);
3095 cascoda_api_upstream = NULL;
3097 ca8210_hw_setup(hw);
3098 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
3100 pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
3106 priv->spi->dev.platform_data = pdata;
3107 ret = ca8210_get_platform_data(priv->spi, pdata);
3109 dev_crit(&spi_device->dev, "ca8210_get_platform_data failed\n");
3113 ret = ca8210_dev_com_init(priv);
3115 dev_crit(&spi_device->dev, "ca8210_dev_com_init failed\n");
3118 ret = ca8210_reset_init(priv->spi);
3120 dev_crit(&spi_device->dev, "ca8210_reset_init failed\n");
3124 ret = ca8210_interrupt_init(priv->spi);
3126 dev_crit(&spi_device->dev, "ca8210_interrupt_init failed\n");
3132 ca8210_reset_send(priv->spi, 1);
3134 ret = tdme_chipinit(priv->spi);
3136 dev_crit(&spi_device->dev, "tdme_chipinit failed\n");
3140 if (pdata->extclockenable) {
3141 ret = ca8210_config_extern_clk(pdata, priv->spi, 1);
3145 "ca8210_config_extern_clk failed\n"
3149 ret = ca8210_register_ext_clock(priv->spi);
3153 "ca8210_register_ext_clock failed\n"
3159 ret = ieee802154_register_hw(hw);
3161 dev_crit(&spi_device->dev, "ieee802154_register_hw failed\n");
3164 priv->hw_registered = true;
3168 msleep(100); /* wait for pending spi transfers to complete */
3169 ca8210_remove(spi_device);
3170 return link_to_linux_err(ret);
3173 static const struct of_device_id ca8210_of_ids[] = {
3174 {.compatible = "cascoda,ca8210", },
3177 MODULE_DEVICE_TABLE(of, ca8210_of_ids);
3179 static struct spi_driver ca8210_spi_driver = {
3181 .name = DRIVER_NAME,
3182 .of_match_table = ca8210_of_ids,
3184 .probe = ca8210_probe,
3185 .remove = ca8210_remove
3188 module_spi_driver(ca8210_spi_driver);
3190 MODULE_AUTHOR("Harry Morris <h.morris@cascoda.com>");
3191 MODULE_DESCRIPTION("CA-8210 SoftMAC driver");
3192 MODULE_LICENSE("Dual BSD/GPL");
3193 MODULE_VERSION("1.0");