Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle |
| 3 | * |
| 4 | * Written 2013 by Werner Almesberger <werner@almesberger.net> |
| 5 | * |
Stefan Schmidt | 151c37b | 2016-04-19 16:28:55 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org> |
| 7 | * |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation, version 2 |
| 11 | * |
| 12 | * Based on at86rf230.c and spi_atusb.c. |
| 13 | * at86rf230.c is |
| 14 | * Copyright (C) 2009 Siemens AG |
| 15 | * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com> |
| 16 | * |
| 17 | * spi_atusb.c is |
| 18 | * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com> |
| 19 | * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org> |
| 20 | * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net> |
| 21 | * |
| 22 | * USB initialization is |
| 23 | * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com> |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 24 | * |
| 25 | * Busware HUL support is |
| 26 | * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at> |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/usb.h> |
| 34 | #include <linux/skbuff.h> |
| 35 | |
| 36 | #include <net/cfg802154.h> |
| 37 | #include <net/mac802154.h> |
| 38 | |
| 39 | #include "at86rf230.h" |
| 40 | #include "atusb.h" |
| 41 | |
| 42 | #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */ |
| 43 | |
| 44 | #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */ |
| 45 | #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */ |
| 46 | #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */ |
| 47 | |
| 48 | struct atusb { |
| 49 | struct ieee802154_hw *hw; |
| 50 | struct usb_device *usb_dev; |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 51 | struct atusb_chip_data *data; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 52 | int shutdown; /* non-zero if shutting down */ |
| 53 | int err; /* set by first error */ |
| 54 | |
| 55 | /* RX variables */ |
| 56 | struct delayed_work work; /* memory allocations */ |
| 57 | struct usb_anchor idle_urbs; /* URBs waiting to be submitted */ |
| 58 | struct usb_anchor rx_urbs; /* URBs waiting for reception */ |
| 59 | |
| 60 | /* TX variables */ |
| 61 | struct usb_ctrlrequest tx_dr; |
| 62 | struct urb *tx_urb; |
| 63 | struct sk_buff *tx_skb; |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 64 | u8 tx_ack_seq; /* current TX ACK sequence number */ |
Stefan Schmidt | 4655156 | 2016-12-05 14:47:18 +0100 | [diff] [blame] | 65 | |
| 66 | /* Firmware variable */ |
| 67 | unsigned char fw_ver_maj; /* Firmware major version number */ |
| 68 | unsigned char fw_ver_min; /* Firmware minor version number */ |
| 69 | unsigned char fw_hw_type; /* Firmware hardware type */ |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 72 | struct atusb_chip_data { |
| 73 | u16 t_channel_switch; |
| 74 | int rssi_base_val; |
| 75 | |
| 76 | int (*set_channel)(struct ieee802154_hw*, u8, u8); |
| 77 | int (*set_txpower)(struct ieee802154_hw*, s32); |
| 78 | }; |
| 79 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 80 | /* ----- USB commands without data ----------------------------------------- */ |
| 81 | |
| 82 | /* To reduce the number of error checks in the code, we record the first error |
| 83 | * in atusb->err and reject all subsequent requests until the error is cleared. |
| 84 | */ |
| 85 | |
| 86 | static int atusb_control_msg(struct atusb *atusb, unsigned int pipe, |
| 87 | __u8 request, __u8 requesttype, |
| 88 | __u16 value, __u16 index, |
| 89 | void *data, __u16 size, int timeout) |
| 90 | { |
| 91 | struct usb_device *usb_dev = atusb->usb_dev; |
| 92 | int ret; |
| 93 | |
| 94 | if (atusb->err) |
| 95 | return atusb->err; |
| 96 | |
| 97 | ret = usb_control_msg(usb_dev, pipe, request, requesttype, |
| 98 | value, index, data, size, timeout); |
| 99 | if (ret < 0) { |
| 100 | atusb->err = ret; |
| 101 | dev_err(&usb_dev->dev, |
| 102 | "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n", |
| 103 | request, value, index, ret); |
| 104 | } |
| 105 | return ret; |
| 106 | } |
| 107 | |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 108 | static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg) |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 109 | { |
| 110 | struct usb_device *usb_dev = atusb->usb_dev; |
| 111 | |
| 112 | dev_dbg(&usb_dev->dev, "atusb_command: cmd = 0x%x\n", cmd); |
| 113 | return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0), |
| 114 | cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000); |
| 115 | } |
| 116 | |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 117 | static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value) |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 118 | { |
| 119 | struct usb_device *usb_dev = atusb->usb_dev; |
| 120 | |
| 121 | dev_dbg(&usb_dev->dev, "atusb_write_reg: 0x%02x <- 0x%02x\n", |
| 122 | reg, value); |
| 123 | return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0), |
| 124 | ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV, |
| 125 | value, reg, NULL, 0, 1000); |
| 126 | } |
| 127 | |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 128 | static int atusb_read_reg(struct atusb *atusb, u8 reg) |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 129 | { |
| 130 | struct usb_device *usb_dev = atusb->usb_dev; |
| 131 | int ret; |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 132 | u8 *buffer; |
| 133 | u8 value; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 134 | |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 135 | buffer = kmalloc(1, GFP_KERNEL); |
| 136 | if (!buffer) |
| 137 | return -ENOMEM; |
| 138 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 139 | dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg); |
| 140 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 141 | ATUSB_REG_READ, ATUSB_REQ_FROM_DEV, |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 142 | 0, reg, buffer, 1, 1000); |
| 143 | |
| 144 | if (ret >= 0) { |
| 145 | value = buffer[0]; |
| 146 | kfree(buffer); |
| 147 | return value; |
| 148 | } else { |
| 149 | kfree(buffer); |
| 150 | return ret; |
| 151 | } |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 154 | static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask, |
| 155 | u8 shift, u8 value) |
Stefan Schmidt | bdc7873 | 2015-05-29 10:51:25 +0200 | [diff] [blame] | 156 | { |
| 157 | struct usb_device *usb_dev = atusb->usb_dev; |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 158 | u8 orig, tmp; |
Stefan Schmidt | bdc7873 | 2015-05-29 10:51:25 +0200 | [diff] [blame] | 159 | int ret = 0; |
| 160 | |
| 161 | dev_dbg(&usb_dev->dev, "atusb_write_subreg: 0x%02x <- 0x%02x\n", |
| 162 | reg, value); |
| 163 | |
| 164 | orig = atusb_read_reg(atusb, reg); |
| 165 | |
| 166 | /* Write the value only into that part of the register which is allowed |
| 167 | * by the mask. All other bits stay as before. |
| 168 | */ |
| 169 | tmp = orig & ~mask; |
| 170 | tmp |= (value << shift) & mask; |
| 171 | |
| 172 | if (tmp != orig) |
| 173 | ret = atusb_write_reg(atusb, reg, tmp); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 178 | static int atusb_read_subreg(struct atusb *lp, |
| 179 | unsigned int addr, unsigned int mask, |
| 180 | unsigned int shift) |
| 181 | { |
| 182 | int rc; |
| 183 | |
| 184 | rc = atusb_read_reg(lp, addr); |
| 185 | rc = (rc & mask) >> shift; |
| 186 | |
| 187 | return rc; |
| 188 | } |
| 189 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 190 | static int atusb_get_and_clear_error(struct atusb *atusb) |
| 191 | { |
| 192 | int err = atusb->err; |
| 193 | |
| 194 | atusb->err = 0; |
| 195 | return err; |
| 196 | } |
| 197 | |
| 198 | /* ----- skb allocation ---------------------------------------------------- */ |
| 199 | |
| 200 | #define MAX_PSDU 127 |
| 201 | #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */ |
| 202 | |
| 203 | #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb) |
| 204 | |
| 205 | static void atusb_in(struct urb *urb); |
| 206 | |
| 207 | static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb) |
| 208 | { |
| 209 | struct usb_device *usb_dev = atusb->usb_dev; |
| 210 | struct sk_buff *skb = urb->context; |
| 211 | int ret; |
| 212 | |
| 213 | if (!skb) { |
| 214 | skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL); |
| 215 | if (!skb) { |
| 216 | dev_warn_ratelimited(&usb_dev->dev, |
| 217 | "atusb_in: can't allocate skb\n"); |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | skb_put(skb, MAX_RX_XFER); |
| 221 | SKB_ATUSB(skb) = atusb; |
| 222 | } |
| 223 | |
| 224 | usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1), |
| 225 | skb->data, MAX_RX_XFER, atusb_in, skb); |
| 226 | usb_anchor_urb(urb, &atusb->rx_urbs); |
| 227 | |
| 228 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 229 | if (ret) { |
| 230 | usb_unanchor_urb(urb); |
| 231 | kfree_skb(skb); |
| 232 | urb->context = NULL; |
| 233 | } |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | static void atusb_work_urbs(struct work_struct *work) |
| 238 | { |
| 239 | struct atusb *atusb = |
| 240 | container_of(to_delayed_work(work), struct atusb, work); |
| 241 | struct usb_device *usb_dev = atusb->usb_dev; |
| 242 | struct urb *urb; |
| 243 | int ret; |
| 244 | |
| 245 | if (atusb->shutdown) |
| 246 | return; |
| 247 | |
| 248 | do { |
| 249 | urb = usb_get_from_anchor(&atusb->idle_urbs); |
| 250 | if (!urb) |
| 251 | return; |
| 252 | ret = atusb_submit_rx_urb(atusb, urb); |
| 253 | } while (!ret); |
| 254 | |
| 255 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 256 | dev_warn_ratelimited(&usb_dev->dev, |
| 257 | "atusb_in: can't allocate/submit URB (%d)\n", ret); |
| 258 | schedule_delayed_work(&atusb->work, |
| 259 | msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1); |
| 260 | } |
| 261 | |
| 262 | /* ----- Asynchronous USB -------------------------------------------------- */ |
| 263 | |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 264 | static void atusb_tx_done(struct atusb *atusb, u8 seq) |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 265 | { |
| 266 | struct usb_device *usb_dev = atusb->usb_dev; |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 267 | u8 expect = atusb->tx_ack_seq; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 268 | |
| 269 | dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect); |
| 270 | if (seq == expect) { |
| 271 | /* TODO check for ifs handling in firmware */ |
| 272 | ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false); |
| 273 | } else { |
| 274 | /* TODO I experience this case when atusb has a tx complete |
| 275 | * irq before probing, we should fix the firmware it's an |
| 276 | * unlikely case now that seq == expect is then true, but can |
| 277 | * happen and fail with a tx_skb = NULL; |
| 278 | */ |
| 279 | ieee802154_wake_queue(atusb->hw); |
| 280 | if (atusb->tx_skb) |
| 281 | dev_kfree_skb_irq(atusb->tx_skb); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static void atusb_in_good(struct urb *urb) |
| 286 | { |
| 287 | struct usb_device *usb_dev = urb->dev; |
| 288 | struct sk_buff *skb = urb->context; |
| 289 | struct atusb *atusb = SKB_ATUSB(skb); |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 290 | u8 len, lqi; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 291 | |
| 292 | if (!urb->actual_length) { |
| 293 | dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n"); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | len = *skb->data; |
| 298 | |
| 299 | if (urb->actual_length == 1) { |
| 300 | atusb_tx_done(atusb, len); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | if (len + 1 > urb->actual_length - 1) { |
| 305 | dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n", |
| 306 | len, urb->actual_length); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | if (!ieee802154_is_valid_psdu_len(len)) { |
| 311 | dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n"); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | lqi = skb->data[len + 1]; |
| 316 | dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi); |
| 317 | skb_pull(skb, 1); /* remove PHR */ |
| 318 | skb_trim(skb, len); /* get payload only */ |
| 319 | ieee802154_rx_irqsafe(atusb->hw, skb, lqi); |
| 320 | urb->context = NULL; /* skb is gone */ |
| 321 | } |
| 322 | |
| 323 | static void atusb_in(struct urb *urb) |
| 324 | { |
| 325 | struct usb_device *usb_dev = urb->dev; |
| 326 | struct sk_buff *skb = urb->context; |
| 327 | struct atusb *atusb = SKB_ATUSB(skb); |
| 328 | |
| 329 | dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n", |
| 330 | urb->status, urb->actual_length); |
| 331 | if (urb->status) { |
| 332 | if (urb->status == -ENOENT) { /* being killed */ |
| 333 | kfree_skb(skb); |
| 334 | urb->context = NULL; |
| 335 | return; |
| 336 | } |
| 337 | dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status); |
| 338 | } else { |
| 339 | atusb_in_good(urb); |
| 340 | } |
| 341 | |
| 342 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 343 | if (!atusb->shutdown) |
| 344 | schedule_delayed_work(&atusb->work, 0); |
| 345 | } |
| 346 | |
| 347 | /* ----- URB allocation/deallocation --------------------------------------- */ |
| 348 | |
| 349 | static void atusb_free_urbs(struct atusb *atusb) |
| 350 | { |
| 351 | struct urb *urb; |
| 352 | |
| 353 | while (1) { |
| 354 | urb = usb_get_from_anchor(&atusb->idle_urbs); |
| 355 | if (!urb) |
| 356 | break; |
Markus Elfring | 4188146 | 2015-12-10 23:44:34 +0100 | [diff] [blame] | 357 | kfree_skb(urb->context); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 358 | usb_free_urb(urb); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | static int atusb_alloc_urbs(struct atusb *atusb, int n) |
| 363 | { |
| 364 | struct urb *urb; |
| 365 | |
| 366 | while (n) { |
| 367 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 368 | if (!urb) { |
| 369 | atusb_free_urbs(atusb); |
| 370 | return -ENOMEM; |
| 371 | } |
| 372 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 373 | n--; |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* ----- IEEE 802.15.4 interface operations -------------------------------- */ |
| 379 | |
| 380 | static void atusb_xmit_complete(struct urb *urb) |
| 381 | { |
| 382 | dev_dbg(&urb->dev->dev, "atusb_xmit urb completed"); |
| 383 | } |
| 384 | |
| 385 | static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb) |
| 386 | { |
| 387 | struct atusb *atusb = hw->priv; |
| 388 | struct usb_device *usb_dev = atusb->usb_dev; |
| 389 | int ret; |
| 390 | |
| 391 | dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len); |
| 392 | atusb->tx_skb = skb; |
| 393 | atusb->tx_ack_seq++; |
| 394 | atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq); |
| 395 | atusb->tx_dr.wLength = cpu_to_le16(skb->len); |
| 396 | |
| 397 | usb_fill_control_urb(atusb->tx_urb, usb_dev, |
| 398 | usb_sndctrlpipe(usb_dev, 0), |
| 399 | (unsigned char *)&atusb->tx_dr, skb->data, |
| 400 | skb->len, atusb_xmit_complete, NULL); |
| 401 | ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC); |
| 402 | dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret); |
| 403 | return ret; |
| 404 | } |
| 405 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 406 | static int atusb_ed(struct ieee802154_hw *hw, u8 *level) |
| 407 | { |
Stefan Schmidt | 2d8cbd3 | 2015-05-21 16:51:37 +0200 | [diff] [blame] | 408 | BUG_ON(!level); |
| 409 | *level = 0xbe; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw, |
| 414 | struct ieee802154_hw_addr_filt *filt, |
| 415 | unsigned long changed) |
| 416 | { |
| 417 | struct atusb *atusb = hw->priv; |
| 418 | struct device *dev = &atusb->usb_dev->dev; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 419 | |
| 420 | if (changed & IEEE802154_AFILT_SADDR_CHANGED) { |
| 421 | u16 addr = le16_to_cpu(filt->short_addr); |
| 422 | |
| 423 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n"); |
| 424 | atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr); |
| 425 | atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8); |
| 426 | } |
| 427 | |
| 428 | if (changed & IEEE802154_AFILT_PANID_CHANGED) { |
| 429 | u16 pan = le16_to_cpu(filt->pan_id); |
| 430 | |
| 431 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n"); |
| 432 | atusb_write_reg(atusb, RG_PAN_ID_0, pan); |
| 433 | atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8); |
| 434 | } |
| 435 | |
| 436 | if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) { |
| 437 | u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN]; |
| 438 | |
| 439 | memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN); |
| 440 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n"); |
| 441 | for (i = 0; i < 8; i++) |
| 442 | atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]); |
| 443 | } |
| 444 | |
| 445 | if (changed & IEEE802154_AFILT_PANC_CHANGED) { |
| 446 | dev_vdbg(dev, |
| 447 | "atusb_set_hw_addr_filt called for panc change\n"); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 448 | if (filt->pan_coord) |
Stefan Schmidt | bdc7873 | 2015-05-29 10:51:25 +0200 | [diff] [blame] | 449 | atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 450 | else |
Stefan Schmidt | bdc7873 | 2015-05-29 10:51:25 +0200 | [diff] [blame] | 451 | atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | return atusb_get_and_clear_error(atusb); |
| 455 | } |
| 456 | |
| 457 | static int atusb_start(struct ieee802154_hw *hw) |
| 458 | { |
| 459 | struct atusb *atusb = hw->priv; |
| 460 | struct usb_device *usb_dev = atusb->usb_dev; |
| 461 | int ret; |
| 462 | |
| 463 | dev_dbg(&usb_dev->dev, "atusb_start\n"); |
| 464 | schedule_delayed_work(&atusb->work, 0); |
| 465 | atusb_command(atusb, ATUSB_RX_MODE, 1); |
| 466 | ret = atusb_get_and_clear_error(atusb); |
| 467 | if (ret < 0) |
| 468 | usb_kill_anchored_urbs(&atusb->idle_urbs); |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | static void atusb_stop(struct ieee802154_hw *hw) |
| 473 | { |
| 474 | struct atusb *atusb = hw->priv; |
| 475 | struct usb_device *usb_dev = atusb->usb_dev; |
| 476 | |
| 477 | dev_dbg(&usb_dev->dev, "atusb_stop\n"); |
| 478 | usb_kill_anchored_urbs(&atusb->idle_urbs); |
| 479 | atusb_command(atusb, ATUSB_RX_MODE, 0); |
| 480 | atusb_get_and_clear_error(atusb); |
| 481 | } |
| 482 | |
Stefan Schmidt | 8702cb0 | 2015-05-29 10:51:26 +0200 | [diff] [blame] | 483 | #define ATUSB_MAX_TX_POWERS 0xF |
| 484 | static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = { |
| 485 | 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700, |
| 486 | -900, -1200, -1700, |
| 487 | }; |
| 488 | |
| 489 | static int |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 490 | atusb_txpower(struct ieee802154_hw *hw, s32 mbm) |
| 491 | { |
| 492 | struct atusb *atusb = hw->priv; |
| 493 | |
| 494 | if (atusb->data) |
| 495 | return atusb->data->set_txpower(hw, mbm); |
| 496 | else |
| 497 | return -ENOTSUPP; |
| 498 | } |
| 499 | |
| 500 | static int |
Stefan Schmidt | 8702cb0 | 2015-05-29 10:51:26 +0200 | [diff] [blame] | 501 | atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm) |
| 502 | { |
| 503 | struct atusb *atusb = hw->priv; |
| 504 | u32 i; |
| 505 | |
| 506 | for (i = 0; i < hw->phy->supported.tx_powers_size; i++) { |
| 507 | if (hw->phy->supported.tx_powers[i] == mbm) |
| 508 | return atusb_write_subreg(atusb, SR_TX_PWR_23X, i); |
| 509 | } |
| 510 | |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 514 | static int |
| 515 | hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm) |
| 516 | { |
| 517 | u32 i; |
| 518 | |
| 519 | for (i = 0; i < hw->phy->supported.tx_powers_size; i++) { |
| 520 | if (hw->phy->supported.tx_powers[i] == mbm) |
| 521 | return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i); |
| 522 | } |
| 523 | |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Stefan Schmidt | 0f4715c | 2016-04-19 16:28:53 +0200 | [diff] [blame] | 527 | #define ATUSB_MAX_ED_LEVELS 0xF |
| 528 | static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = { |
| 529 | -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300, |
| 530 | -7100, -6900, -6700, -6500, -6300, -6100, |
| 531 | }; |
| 532 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 533 | #define AT86RF212_MAX_TX_POWERS 0x1F |
| 534 | static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = { |
| 535 | 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700, |
| 536 | -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700, |
| 537 | -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600, |
| 538 | }; |
| 539 | |
| 540 | #define AT86RF2XX_MAX_ED_LEVELS 0xF |
| 541 | static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = { |
| 542 | -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, |
| 543 | -8000, -7800, -7600, -7400, -7200, -7000, |
| 544 | }; |
| 545 | |
| 546 | static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = { |
| 547 | -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, |
| 548 | -7800, -7600, -7400, -7200, -7000, -6800, |
| 549 | }; |
| 550 | |
Stefan Schmidt | 0f4715c | 2016-04-19 16:28:53 +0200 | [diff] [blame] | 551 | static int |
Stefan Schmidt | 308dbb7 | 2016-04-19 16:28:54 +0200 | [diff] [blame] | 552 | atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca) |
| 553 | { |
| 554 | struct atusb *atusb = hw->priv; |
| 555 | u8 val; |
| 556 | |
| 557 | /* mapping 802.15.4 to driver spec */ |
| 558 | switch (cca->mode) { |
| 559 | case NL802154_CCA_ENERGY: |
| 560 | val = 1; |
| 561 | break; |
| 562 | case NL802154_CCA_CARRIER: |
| 563 | val = 2; |
| 564 | break; |
| 565 | case NL802154_CCA_ENERGY_CARRIER: |
| 566 | switch (cca->opt) { |
| 567 | case NL802154_CCA_OPT_ENERGY_CARRIER_AND: |
| 568 | val = 3; |
| 569 | break; |
| 570 | case NL802154_CCA_OPT_ENERGY_CARRIER_OR: |
| 571 | val = 0; |
| 572 | break; |
| 573 | default: |
| 574 | return -EINVAL; |
| 575 | } |
| 576 | break; |
| 577 | default: |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | return atusb_write_subreg(atusb, SR_CCA_MODE, val); |
| 582 | } |
| 583 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 584 | static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val) |
| 585 | { |
| 586 | unsigned int cca_ed_thres; |
| 587 | |
| 588 | cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES); |
| 589 | |
| 590 | switch (rssi_base_val) { |
| 591 | case -98: |
| 592 | lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98; |
| 593 | lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98); |
| 594 | lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres]; |
| 595 | break; |
| 596 | case -100: |
| 597 | lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100; |
| 598 | lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100); |
| 599 | lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres]; |
| 600 | break; |
| 601 | default: |
| 602 | WARN_ON(1); |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
Stefan Schmidt | 308dbb7 | 2016-04-19 16:28:54 +0200 | [diff] [blame] | 608 | static int |
Stefan Schmidt | 0f4715c | 2016-04-19 16:28:53 +0200 | [diff] [blame] | 609 | atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm) |
| 610 | { |
| 611 | struct atusb *atusb = hw->priv; |
| 612 | u32 i; |
| 613 | |
| 614 | for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) { |
| 615 | if (hw->phy->supported.cca_ed_levels[i] == mbm) |
| 616 | return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i); |
| 617 | } |
| 618 | |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 622 | static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
| 623 | { |
| 624 | struct atusb *atusb = hw->priv; |
| 625 | int ret = -ENOTSUPP; |
| 626 | |
| 627 | if (atusb->data) { |
| 628 | ret = atusb->data->set_channel(hw, page, channel); |
| 629 | /* @@@ ugly synchronization */ |
| 630 | msleep(atusb->data->t_channel_switch); |
| 631 | } |
| 632 | |
| 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
| 637 | { |
| 638 | struct atusb *atusb = hw->priv; |
| 639 | int ret; |
| 640 | |
| 641 | ret = atusb_write_subreg(atusb, SR_CHANNEL, channel); |
| 642 | if (ret < 0) |
| 643 | return ret; |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
| 648 | { |
| 649 | int rc; |
| 650 | int rssi_base_val; |
| 651 | |
| 652 | struct atusb *lp = hw->priv; |
| 653 | |
| 654 | if (channel == 0) |
| 655 | rc = atusb_write_subreg(lp, SR_SUB_MODE, 0); |
| 656 | else |
| 657 | rc = atusb_write_subreg(lp, SR_SUB_MODE, 1); |
| 658 | if (rc < 0) |
| 659 | return rc; |
| 660 | |
| 661 | if (page == 0) { |
| 662 | rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0); |
| 663 | rssi_base_val = -100; |
| 664 | } else { |
| 665 | rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1); |
| 666 | rssi_base_val = -98; |
| 667 | } |
| 668 | if (rc < 0) |
| 669 | return rc; |
| 670 | |
| 671 | rc = hulusb_set_cca_ed_level(lp, rssi_base_val); |
| 672 | if (rc < 0) |
| 673 | return rc; |
| 674 | |
| 675 | /* This sets the symbol_duration according frequency on the 212. |
| 676 | * TODO move this handling while set channel and page in cfg802154. |
| 677 | * We can do that, this timings are according 802.15.4 standard. |
| 678 | * If we do that in cfg802154, this is a more generic calculation. |
| 679 | * |
| 680 | * This should also protected from ifs_timer. Means cancel timer and |
| 681 | * init with a new value. For now, this is okay. |
| 682 | */ |
| 683 | if (channel == 0) { |
| 684 | if (page == 0) { |
| 685 | /* SUB:0 and BPSK:0 -> BPSK-20 */ |
| 686 | lp->hw->phy->symbol_duration = 50; |
| 687 | } else { |
| 688 | /* SUB:1 and BPSK:0 -> BPSK-40 */ |
| 689 | lp->hw->phy->symbol_duration = 25; |
| 690 | } |
| 691 | } else { |
| 692 | if (page == 0) |
| 693 | /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */ |
| 694 | lp->hw->phy->symbol_duration = 40; |
| 695 | else |
| 696 | /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */ |
| 697 | lp->hw->phy->symbol_duration = 16; |
| 698 | } |
| 699 | |
| 700 | lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD * |
| 701 | lp->hw->phy->symbol_duration; |
| 702 | lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD * |
| 703 | lp->hw->phy->symbol_duration; |
| 704 | |
| 705 | return atusb_write_subreg(lp, SR_CHANNEL, channel); |
| 706 | } |
| 707 | |
Stefan Schmidt | c61c9bd | 2015-05-29 10:51:27 +0200 | [diff] [blame] | 708 | static int |
Stefan Schmidt | fb7c579 | 2016-04-19 16:28:52 +0200 | [diff] [blame] | 709 | atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries) |
| 710 | { |
| 711 | struct atusb *atusb = hw->priv; |
| 712 | int ret; |
| 713 | |
| 714 | ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be); |
| 715 | if (ret) |
| 716 | return ret; |
| 717 | |
| 718 | ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be); |
| 719 | if (ret) |
| 720 | return ret; |
| 721 | |
| 722 | return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries); |
| 723 | } |
| 724 | |
| 725 | static int |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 726 | hulusb_set_lbt(struct ieee802154_hw *hw, bool on) |
| 727 | { |
| 728 | struct atusb *atusb = hw->priv; |
| 729 | |
| 730 | return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on); |
| 731 | } |
| 732 | |
| 733 | static int |
Stefan Schmidt | 5d82288 | 2016-12-05 14:47:20 +0100 | [diff] [blame] | 734 | atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries) |
| 735 | { |
| 736 | struct atusb *atusb = hw->priv; |
Stefan Schmidt | 5d82288 | 2016-12-05 14:47:20 +0100 | [diff] [blame] | 737 | |
| 738 | return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries); |
| 739 | } |
| 740 | |
| 741 | static int |
Stefan Schmidt | c61c9bd | 2015-05-29 10:51:27 +0200 | [diff] [blame] | 742 | atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on) |
| 743 | { |
| 744 | struct atusb *atusb = hw->priv; |
| 745 | int ret; |
| 746 | |
| 747 | if (on) { |
| 748 | ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1); |
| 749 | if (ret < 0) |
| 750 | return ret; |
| 751 | |
| 752 | ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1); |
| 753 | if (ret < 0) |
| 754 | return ret; |
| 755 | } else { |
| 756 | ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0); |
| 757 | if (ret < 0) |
| 758 | return ret; |
| 759 | |
| 760 | ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0); |
| 761 | if (ret < 0) |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
Colin Ian King | 06ff5da | 2017-09-26 14:18:36 +0100 | [diff] [blame] | 768 | static struct atusb_chip_data atusb_chip_data = { |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 769 | .t_channel_switch = 1, |
| 770 | .rssi_base_val = -91, |
| 771 | .set_txpower = atusb_set_txpower, |
| 772 | .set_channel = atusb_set_channel, |
| 773 | }; |
| 774 | |
Colin Ian King | 06ff5da | 2017-09-26 14:18:36 +0100 | [diff] [blame] | 775 | static struct atusb_chip_data hulusb_chip_data = { |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 776 | .t_channel_switch = 11, |
| 777 | .rssi_base_val = -100, |
| 778 | .set_txpower = hulusb_set_txpower, |
| 779 | .set_channel = hulusb_set_channel, |
| 780 | }; |
| 781 | |
Bhumika Goyal | e796f49 | 2016-11-22 02:00:14 +0530 | [diff] [blame] | 782 | static const struct ieee802154_ops atusb_ops = { |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 783 | .owner = THIS_MODULE, |
| 784 | .xmit_async = atusb_xmit, |
| 785 | .ed = atusb_ed, |
| 786 | .set_channel = atusb_channel, |
| 787 | .start = atusb_start, |
| 788 | .stop = atusb_stop, |
| 789 | .set_hw_addr_filt = atusb_set_hw_addr_filt, |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 790 | .set_txpower = atusb_txpower, |
| 791 | .set_lbt = hulusb_set_lbt, |
Stefan Schmidt | 308dbb7 | 2016-04-19 16:28:54 +0200 | [diff] [blame] | 792 | .set_cca_mode = atusb_set_cca_mode, |
Stefan Schmidt | 0f4715c | 2016-04-19 16:28:53 +0200 | [diff] [blame] | 793 | .set_cca_ed_level = atusb_set_cca_ed_level, |
Stefan Schmidt | fb7c579 | 2016-04-19 16:28:52 +0200 | [diff] [blame] | 794 | .set_csma_params = atusb_set_csma_params, |
Stefan Schmidt | 5d82288 | 2016-12-05 14:47:20 +0100 | [diff] [blame] | 795 | .set_frame_retries = atusb_set_frame_retries, |
Stefan Schmidt | c61c9bd | 2015-05-29 10:51:27 +0200 | [diff] [blame] | 796 | .set_promiscuous_mode = atusb_set_promiscuous_mode, |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 797 | }; |
| 798 | |
| 799 | /* ----- Firmware and chip version information ----------------------------- */ |
| 800 | |
| 801 | static int atusb_get_and_show_revision(struct atusb *atusb) |
| 802 | { |
| 803 | struct usb_device *usb_dev = atusb->usb_dev; |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 804 | char *hw_name; |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 805 | unsigned char *buffer; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 806 | int ret; |
| 807 | |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 808 | buffer = kmalloc(3, GFP_KERNEL); |
| 809 | if (!buffer) |
| 810 | return -ENOMEM; |
| 811 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 812 | /* Get a couple of the ATMega Firmware values */ |
| 813 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 814 | ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0, |
| 815 | buffer, 3, 1000); |
Stefan Schmidt | 4655156 | 2016-12-05 14:47:18 +0100 | [diff] [blame] | 816 | if (ret >= 0) { |
| 817 | atusb->fw_ver_maj = buffer[0]; |
| 818 | atusb->fw_ver_min = buffer[1]; |
| 819 | atusb->fw_hw_type = buffer[2]; |
| 820 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 821 | switch (atusb->fw_hw_type) { |
| 822 | case ATUSB_HW_TYPE_100813: |
| 823 | case ATUSB_HW_TYPE_101216: |
| 824 | case ATUSB_HW_TYPE_110131: |
| 825 | hw_name = "ATUSB"; |
| 826 | atusb->data = &atusb_chip_data; |
| 827 | break; |
| 828 | case ATUSB_HW_TYPE_RZUSB: |
| 829 | hw_name = "RZUSB"; |
| 830 | atusb->data = &atusb_chip_data; |
| 831 | break; |
| 832 | case ATUSB_HW_TYPE_HULUSB: |
| 833 | hw_name = "HULUSB"; |
| 834 | atusb->data = &hulusb_chip_data; |
| 835 | break; |
| 836 | default: |
| 837 | hw_name = "UNKNOWN"; |
| 838 | atusb->err = -ENOTSUPP; |
| 839 | ret = -ENOTSUPP; |
| 840 | break; |
| 841 | } |
| 842 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 843 | dev_info(&usb_dev->dev, |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 844 | "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n", |
| 845 | atusb->fw_ver_maj, atusb->fw_ver_min, hw_name, atusb->fw_hw_type); |
Stefan Schmidt | 4655156 | 2016-12-05 14:47:18 +0100 | [diff] [blame] | 846 | } |
| 847 | if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) { |
Stefan Schmidt | 33a238a | 2015-05-21 16:51:35 +0200 | [diff] [blame] | 848 | dev_info(&usb_dev->dev, |
Stefan Schmidt | 4655156 | 2016-12-05 14:47:18 +0100 | [diff] [blame] | 849 | "Firmware version (%u.%u) predates our first public release.", |
| 850 | atusb->fw_ver_maj, atusb->fw_ver_min); |
Stefan Schmidt | 33a238a | 2015-05-21 16:51:35 +0200 | [diff] [blame] | 851 | dev_info(&usb_dev->dev, "Please update to version 0.2 or newer"); |
| 852 | } |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 853 | |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 854 | kfree(buffer); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | static int atusb_get_and_show_build(struct atusb *atusb) |
| 859 | { |
| 860 | struct usb_device *usb_dev = atusb->usb_dev; |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 861 | char *build; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 862 | int ret; |
| 863 | |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 864 | build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL); |
| 865 | if (!build) |
| 866 | return -ENOMEM; |
| 867 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 868 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 869 | ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0, |
| 870 | build, ATUSB_BUILD_SIZE, 1000); |
| 871 | if (ret >= 0) { |
| 872 | build[ret] = 0; |
| 873 | dev_info(&usb_dev->dev, "Firmware: build %s\n", build); |
| 874 | } |
| 875 | |
Stefan Schmidt | 05a974e | 2016-12-15 18:40:14 +0100 | [diff] [blame] | 876 | kfree(build); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 877 | return ret; |
| 878 | } |
| 879 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 880 | static int atusb_get_and_conf_chip(struct atusb *atusb) |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 881 | { |
| 882 | struct usb_device *usb_dev = atusb->usb_dev; |
Stefan Schmidt | 909dcf9 | 2017-09-22 14:13:55 +0200 | [diff] [blame^] | 883 | u8 man_id_0, man_id_1, part_num, version_num; |
Alexander Aring | 9def9af | 2015-09-05 12:27:59 +0200 | [diff] [blame] | 884 | const char *chip; |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 885 | struct ieee802154_hw *hw = atusb->hw; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 886 | |
| 887 | man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0); |
| 888 | man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1); |
| 889 | part_num = atusb_read_reg(atusb, RG_PART_NUM); |
| 890 | version_num = atusb_read_reg(atusb, RG_VERSION_NUM); |
| 891 | |
| 892 | if (atusb->err) |
| 893 | return atusb->err; |
| 894 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 895 | hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT | |
| 896 | IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS; |
| 897 | |
| 898 | hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL | |
| 899 | WPAN_PHY_FLAG_CCA_MODE; |
| 900 | |
| 901 | hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) | |
| 902 | BIT(NL802154_CCA_CARRIER) | |
| 903 | BIT(NL802154_CCA_ENERGY_CARRIER); |
| 904 | hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) | |
| 905 | BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR); |
| 906 | |
| 907 | hw->phy->cca.mode = NL802154_CCA_ENERGY; |
| 908 | |
| 909 | hw->phy->current_page = 0; |
| 910 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 911 | if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) { |
| 912 | dev_err(&usb_dev->dev, |
| 913 | "non-Atmel transceiver xxxx%02x%02x\n", |
| 914 | man_id_1, man_id_0); |
| 915 | goto fail; |
| 916 | } |
Alexander Aring | 9def9af | 2015-09-05 12:27:59 +0200 | [diff] [blame] | 917 | |
| 918 | switch (part_num) { |
| 919 | case 2: |
| 920 | chip = "AT86RF230"; |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 921 | atusb->hw->phy->supported.channels[0] = 0x7FFF800; |
| 922 | atusb->hw->phy->current_channel = 11; /* reset default */ |
| 923 | atusb->hw->phy->symbol_duration = 16; |
| 924 | atusb->hw->phy->supported.tx_powers = atusb_powers; |
| 925 | atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers); |
| 926 | hw->phy->supported.cca_ed_levels = atusb_ed_levels; |
| 927 | hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels); |
Alexander Aring | 9def9af | 2015-09-05 12:27:59 +0200 | [diff] [blame] | 928 | break; |
| 929 | case 3: |
| 930 | chip = "AT86RF231"; |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 931 | atusb->hw->phy->supported.channels[0] = 0x7FFF800; |
| 932 | atusb->hw->phy->current_channel = 11; /* reset default */ |
| 933 | atusb->hw->phy->symbol_duration = 16; |
| 934 | atusb->hw->phy->supported.tx_powers = atusb_powers; |
| 935 | atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers); |
| 936 | hw->phy->supported.cca_ed_levels = atusb_ed_levels; |
| 937 | hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels); |
| 938 | break; |
| 939 | case 7: |
| 940 | chip = "AT86RF212"; |
| 941 | atusb->hw->flags |= IEEE802154_HW_LBT; |
| 942 | atusb->hw->phy->supported.channels[0] = 0x00007FF; |
| 943 | atusb->hw->phy->supported.channels[2] = 0x00007FF; |
| 944 | atusb->hw->phy->current_channel = 5; |
| 945 | atusb->hw->phy->symbol_duration = 25; |
| 946 | atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH; |
| 947 | atusb->hw->phy->supported.tx_powers = at86rf212_powers; |
| 948 | atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers); |
| 949 | atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100; |
| 950 | atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100); |
Alexander Aring | 9def9af | 2015-09-05 12:27:59 +0200 | [diff] [blame] | 951 | break; |
| 952 | default: |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 953 | dev_err(&usb_dev->dev, |
| 954 | "unexpected transceiver, part 0x%02x version 0x%02x\n", |
| 955 | part_num, version_num); |
| 956 | goto fail; |
| 957 | } |
| 958 | |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 959 | hw->phy->transmit_power = hw->phy->supported.tx_powers[0]; |
| 960 | hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7]; |
| 961 | |
Alexander Aring | 9def9af | 2015-09-05 12:27:59 +0200 | [diff] [blame] | 962 | dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 963 | |
| 964 | return 0; |
| 965 | |
| 966 | fail: |
| 967 | atusb->err = -ENODEV; |
| 968 | return -ENODEV; |
| 969 | } |
| 970 | |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 971 | static int atusb_set_extended_addr(struct atusb *atusb) |
| 972 | { |
| 973 | struct usb_device *usb_dev = atusb->usb_dev; |
Stefan Schmidt | 5eb35a6c | 2016-12-15 18:40:16 +0100 | [diff] [blame] | 974 | unsigned char *buffer; |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 975 | __le64 extended_addr; |
| 976 | u64 addr; |
| 977 | int ret; |
| 978 | |
| 979 | /* Firmware versions before 0.3 do not support the EUI64_READ command. |
| 980 | * Just use a random address and be done */ |
| 981 | if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) { |
| 982 | ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr); |
| 983 | return 0; |
| 984 | } |
| 985 | |
Stefan Schmidt | 5eb35a6c | 2016-12-15 18:40:16 +0100 | [diff] [blame] | 986 | buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL); |
| 987 | if (!buffer) |
| 988 | return -ENOMEM; |
| 989 | |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 990 | /* Firmware is new enough so we fetch the address from EEPROM */ |
| 991 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 992 | ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0, |
| 993 | buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000); |
Stefan Schmidt | 2fd2b55 | 2016-12-15 18:40:15 +0100 | [diff] [blame] | 994 | if (ret < 0) { |
| 995 | dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n"); |
| 996 | ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr); |
Stefan Schmidt | 5eb35a6c | 2016-12-15 18:40:16 +0100 | [diff] [blame] | 997 | kfree(buffer); |
Stefan Schmidt | 2fd2b55 | 2016-12-15 18:40:15 +0100 | [diff] [blame] | 998 | return ret; |
| 999 | } |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 1000 | |
| 1001 | memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN); |
| 1002 | /* Check if read address is not empty and the unicast bit is set correctly */ |
| 1003 | if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) { |
| 1004 | dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n"); |
| 1005 | ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr); |
| 1006 | } else { |
| 1007 | atusb->hw->phy->perm_extended_addr = extended_addr; |
| 1008 | addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr); |
| 1009 | dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n", |
| 1010 | &addr); |
| 1011 | } |
| 1012 | |
Stefan Schmidt | 5eb35a6c | 2016-12-15 18:40:16 +0100 | [diff] [blame] | 1013 | kfree(buffer); |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 1014 | return ret; |
| 1015 | } |
| 1016 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1017 | /* ----- Setup ------------------------------------------------------------- */ |
| 1018 | |
| 1019 | static int atusb_probe(struct usb_interface *interface, |
| 1020 | const struct usb_device_id *id) |
| 1021 | { |
| 1022 | struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 1023 | struct ieee802154_hw *hw; |
| 1024 | struct atusb *atusb = NULL; |
| 1025 | int ret = -ENOMEM; |
| 1026 | |
| 1027 | hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops); |
| 1028 | if (!hw) |
| 1029 | return -ENOMEM; |
| 1030 | |
| 1031 | atusb = hw->priv; |
| 1032 | atusb->hw = hw; |
| 1033 | atusb->usb_dev = usb_get_dev(usb_dev); |
| 1034 | usb_set_intfdata(interface, atusb); |
| 1035 | |
| 1036 | atusb->shutdown = 0; |
| 1037 | atusb->err = 0; |
| 1038 | INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs); |
| 1039 | init_usb_anchor(&atusb->idle_urbs); |
| 1040 | init_usb_anchor(&atusb->rx_urbs); |
| 1041 | |
| 1042 | if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS)) |
| 1043 | goto fail; |
| 1044 | |
| 1045 | atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV; |
| 1046 | atusb->tx_dr.bRequest = ATUSB_TX; |
| 1047 | atusb->tx_dr.wValue = cpu_to_le16(0); |
| 1048 | |
| 1049 | atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 1050 | if (!atusb->tx_urb) |
| 1051 | goto fail; |
| 1052 | |
| 1053 | hw->parent = &usb_dev->dev; |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1054 | |
| 1055 | atusb_command(atusb, ATUSB_RF_RESET, 0); |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 1056 | atusb_get_and_conf_chip(atusb); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1057 | atusb_get_and_show_revision(atusb); |
| 1058 | atusb_get_and_show_build(atusb); |
Stefan Schmidt | 6cc33eb | 2016-12-05 14:47:19 +0100 | [diff] [blame] | 1059 | atusb_set_extended_addr(atusb); |
| 1060 | |
Stefan Schmidt | 7635569 | 2017-09-22 10:44:57 +0200 | [diff] [blame] | 1061 | if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0) |
Stefan Schmidt | 8e38b7d | 2017-01-02 16:58:13 +0100 | [diff] [blame] | 1062 | hw->flags |= IEEE802154_HW_FRAME_RETRIES; |
| 1063 | |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1064 | ret = atusb_get_and_clear_error(atusb); |
| 1065 | if (ret) { |
| 1066 | dev_err(&atusb->usb_dev->dev, |
| 1067 | "%s: initialization failed, error = %d\n", |
| 1068 | __func__, ret); |
| 1069 | goto fail; |
| 1070 | } |
| 1071 | |
| 1072 | ret = ieee802154_register_hw(hw); |
| 1073 | if (ret) |
| 1074 | goto fail; |
| 1075 | |
| 1076 | /* If we just powered on, we're now in P_ON and need to enter TRX_OFF |
| 1077 | * explicitly. Any resets after that will send us straight to TRX_OFF, |
| 1078 | * making the command below redundant. |
| 1079 | */ |
| 1080 | atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF); |
| 1081 | msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */ |
| 1082 | |
| 1083 | #if 0 |
| 1084 | /* Calculating the maximum time available to empty the frame buffer |
| 1085 | * on reception: |
| 1086 | * |
| 1087 | * According to [1], the inter-frame gap is |
| 1088 | * R * 20 * 16 us + 128 us |
| 1089 | * where R is a random number from 0 to 7. Furthermore, we have 20 bit |
| 1090 | * times (80 us at 250 kbps) of SHR of the next frame before the |
| 1091 | * transceiver begins storing data in the frame buffer. |
| 1092 | * |
| 1093 | * This yields a minimum time of 208 us between the last data of a |
| 1094 | * frame and the first data of the next frame. This time is further |
| 1095 | * reduced by interrupt latency in the atusb firmware. |
| 1096 | * |
| 1097 | * atusb currently needs about 500 us to retrieve a maximum-sized |
| 1098 | * frame. We therefore have to allow reception of a new frame to begin |
| 1099 | * while we retrieve the previous frame. |
| 1100 | * |
| 1101 | * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based |
| 1102 | * network", Jennic 2006. |
| 1103 | * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf |
| 1104 | */ |
| 1105 | |
Stefan Schmidt | bdc7873 | 2015-05-29 10:51:25 +0200 | [diff] [blame] | 1106 | atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1107 | #endif |
| 1108 | atusb_write_reg(atusb, RG_IRQ_MASK, 0xff); |
| 1109 | |
| 1110 | ret = atusb_get_and_clear_error(atusb); |
| 1111 | if (!ret) |
| 1112 | return 0; |
| 1113 | |
| 1114 | dev_err(&atusb->usb_dev->dev, |
| 1115 | "%s: setup failed, error = %d\n", |
| 1116 | __func__, ret); |
| 1117 | |
| 1118 | ieee802154_unregister_hw(hw); |
| 1119 | fail: |
| 1120 | atusb_free_urbs(atusb); |
| 1121 | usb_kill_urb(atusb->tx_urb); |
| 1122 | usb_free_urb(atusb->tx_urb); |
| 1123 | usb_put_dev(usb_dev); |
| 1124 | ieee802154_free_hw(hw); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | static void atusb_disconnect(struct usb_interface *interface) |
| 1129 | { |
| 1130 | struct atusb *atusb = usb_get_intfdata(interface); |
| 1131 | |
| 1132 | dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n"); |
| 1133 | |
| 1134 | atusb->shutdown = 1; |
| 1135 | cancel_delayed_work_sync(&atusb->work); |
| 1136 | |
| 1137 | usb_kill_anchored_urbs(&atusb->rx_urbs); |
| 1138 | atusb_free_urbs(atusb); |
| 1139 | usb_kill_urb(atusb->tx_urb); |
| 1140 | usb_free_urb(atusb->tx_urb); |
| 1141 | |
| 1142 | ieee802154_unregister_hw(atusb->hw); |
| 1143 | |
| 1144 | ieee802154_free_hw(atusb->hw); |
| 1145 | |
| 1146 | usb_set_intfdata(interface, NULL); |
| 1147 | usb_put_dev(atusb->usb_dev); |
| 1148 | |
| 1149 | pr_debug("atusb_disconnect done\n"); |
| 1150 | } |
| 1151 | |
| 1152 | /* The devices we work with */ |
| 1153 | static const struct usb_device_id atusb_device_table[] = { |
| 1154 | { |
| 1155 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
| 1156 | USB_DEVICE_ID_MATCH_INT_INFO, |
| 1157 | .idVendor = ATUSB_VENDOR_ID, |
| 1158 | .idProduct = ATUSB_PRODUCT_ID, |
| 1159 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC |
| 1160 | }, |
| 1161 | /* end with null element */ |
| 1162 | {} |
| 1163 | }; |
| 1164 | MODULE_DEVICE_TABLE(usb, atusb_device_table); |
| 1165 | |
| 1166 | static struct usb_driver atusb_driver = { |
| 1167 | .name = "atusb", |
| 1168 | .probe = atusb_probe, |
| 1169 | .disconnect = atusb_disconnect, |
| 1170 | .id_table = atusb_device_table, |
| 1171 | }; |
| 1172 | module_usb_driver(atusb_driver); |
| 1173 | |
| 1174 | MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>"); |
| 1175 | MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>"); |
| 1176 | MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>"); |
| 1177 | MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>"); |
Josef Filzmaier | d5dd29e | 2017-09-14 14:32:10 +0200 | [diff] [blame] | 1178 | MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>"); |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1179 | MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver"); |
| 1180 | MODULE_LICENSE("GPL"); |