1 /* 2 * Copyright (c) 2007-2011 Atheros Communications Inc. 3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <linux/module.h> 19 #include <linux/usb.h> 20 21 #include "debug.h" 22 #include "core.h" 23 24 /* usb device object */ 25 struct ath6kl_usb { 26 struct usb_device *udev; 27 struct usb_interface *interface; 28 u8 *diag_cmd_buffer; 29 u8 *diag_resp_buffer; 30 struct ath6kl *ar; 31 }; 32 33 /* diagnostic command defnitions */ 34 #define ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD 1 35 #define ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP 2 36 #define ATH6KL_USB_CONTROL_REQ_DIAG_CMD 3 37 #define ATH6KL_USB_CONTROL_REQ_DIAG_RESP 4 38 39 #define ATH6KL_USB_CTRL_DIAG_CC_READ 0 40 #define ATH6KL_USB_CTRL_DIAG_CC_WRITE 1 41 42 struct ath6kl_usb_ctrl_diag_cmd_write { 43 __le32 cmd; 44 __le32 address; 45 __le32 value; 46 __le32 _pad[1]; 47 } __packed; 48 49 struct ath6kl_usb_ctrl_diag_cmd_read { 50 __le32 cmd; 51 __le32 address; 52 } __packed; 53 54 struct ath6kl_usb_ctrl_diag_resp_read { 55 __le32 value; 56 } __packed; 57 58 #define ATH6KL_USB_MAX_DIAG_CMD (sizeof(struct ath6kl_usb_ctrl_diag_cmd_write)) 59 #define ATH6KL_USB_MAX_DIAG_RESP (sizeof(struct ath6kl_usb_ctrl_diag_resp_read)) 60 61 static void ath6kl_usb_destroy(struct ath6kl_usb *ar_usb) 62 { 63 usb_set_intfdata(ar_usb->interface, NULL); 64 65 kfree(ar_usb->diag_cmd_buffer); 66 kfree(ar_usb->diag_resp_buffer); 67 68 kfree(ar_usb); 69 } 70 71 static struct ath6kl_usb *ath6kl_usb_create(struct usb_interface *interface) 72 { 73 struct ath6kl_usb *ar_usb = NULL; 74 struct usb_device *dev = interface_to_usbdev(interface); 75 int status = 0; 76 77 ar_usb = kzalloc(sizeof(struct ath6kl_usb), GFP_KERNEL); 78 if (ar_usb == NULL) 79 goto fail_ath6kl_usb_create; 80 81 memset(ar_usb, 0, sizeof(struct ath6kl_usb)); 82 usb_set_intfdata(interface, ar_usb); 83 ar_usb->udev = dev; 84 ar_usb->interface = interface; 85 86 ar_usb->diag_cmd_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_CMD, GFP_KERNEL); 87 if (ar_usb->diag_cmd_buffer == NULL) { 88 status = -ENOMEM; 89 goto fail_ath6kl_usb_create; 90 } 91 92 ar_usb->diag_resp_buffer = kzalloc(ATH6KL_USB_MAX_DIAG_RESP, 93 GFP_KERNEL); 94 if (ar_usb->diag_resp_buffer == NULL) { 95 status = -ENOMEM; 96 goto fail_ath6kl_usb_create; 97 } 98 99 fail_ath6kl_usb_create: 100 if (status != 0) { 101 ath6kl_usb_destroy(ar_usb); 102 ar_usb = NULL; 103 } 104 return ar_usb; 105 } 106 107 static void ath6kl_usb_device_detached(struct usb_interface *interface) 108 { 109 struct ath6kl_usb *ar_usb; 110 111 ar_usb = usb_get_intfdata(interface); 112 if (ar_usb == NULL) 113 return; 114 115 ath6kl_stop_txrx(ar_usb->ar); 116 117 ath6kl_core_cleanup(ar_usb->ar); 118 119 ath6kl_usb_destroy(ar_usb); 120 } 121 122 static int ath6kl_usb_submit_ctrl_out(struct ath6kl_usb *ar_usb, 123 u8 req, u16 value, u16 index, void *data, 124 u32 size) 125 { 126 u8 *buf = NULL; 127 int ret; 128 129 if (size > 0) { 130 buf = kmalloc(size, GFP_KERNEL); 131 if (buf == NULL) 132 return -ENOMEM; 133 134 memcpy(buf, data, size); 135 } 136 137 /* note: if successful returns number of bytes transfered */ 138 ret = usb_control_msg(ar_usb->udev, 139 usb_sndctrlpipe(ar_usb->udev, 0), 140 req, 141 USB_DIR_OUT | USB_TYPE_VENDOR | 142 USB_RECIP_DEVICE, value, index, buf, 143 size, 1000); 144 145 if (ret < 0) { 146 ath6kl_dbg(ATH6KL_DBG_USB, "%s failed,result = %d\n", 147 __func__, ret); 148 } 149 150 kfree(buf); 151 152 return 0; 153 } 154 155 static int ath6kl_usb_submit_ctrl_in(struct ath6kl_usb *ar_usb, 156 u8 req, u16 value, u16 index, void *data, 157 u32 size) 158 { 159 u8 *buf = NULL; 160 int ret; 161 162 if (size > 0) { 163 buf = kmalloc(size, GFP_KERNEL); 164 if (buf == NULL) 165 return -ENOMEM; 166 } 167 168 /* note: if successful returns number of bytes transfered */ 169 ret = usb_control_msg(ar_usb->udev, 170 usb_rcvctrlpipe(ar_usb->udev, 0), 171 req, 172 USB_DIR_IN | USB_TYPE_VENDOR | 173 USB_RECIP_DEVICE, value, index, buf, 174 size, 2 * HZ); 175 176 if (ret < 0) { 177 ath6kl_dbg(ATH6KL_DBG_USB, "%s failed,result = %d\n", 178 __func__, ret); 179 } 180 181 memcpy((u8 *) data, buf, size); 182 183 kfree(buf); 184 185 return 0; 186 } 187 188 static int ath6kl_usb_ctrl_msg_exchange(struct ath6kl_usb *ar_usb, 189 u8 req_val, u8 *req_buf, u32 req_len, 190 u8 resp_val, u8 *resp_buf, u32 *resp_len) 191 { 192 int ret; 193 194 /* send command */ 195 ret = ath6kl_usb_submit_ctrl_out(ar_usb, req_val, 0, 0, 196 req_buf, req_len); 197 198 if (ret != 0) 199 return ret; 200 201 if (resp_buf == NULL) { 202 /* no expected response */ 203 return ret; 204 } 205 206 /* get response */ 207 ret = ath6kl_usb_submit_ctrl_in(ar_usb, resp_val, 0, 0, 208 resp_buf, *resp_len); 209 210 return ret; 211 } 212 213 static int ath6kl_usb_diag_read32(struct ath6kl *ar, u32 address, u32 *data) 214 { 215 struct ath6kl_usb *ar_usb = ar->hif_priv; 216 struct ath6kl_usb_ctrl_diag_resp_read *resp; 217 struct ath6kl_usb_ctrl_diag_cmd_read *cmd; 218 u32 resp_len; 219 int ret; 220 221 cmd = (struct ath6kl_usb_ctrl_diag_cmd_read *) ar_usb->diag_cmd_buffer; 222 223 memset(cmd, 0, sizeof(*cmd)); 224 cmd->cmd = ATH6KL_USB_CTRL_DIAG_CC_READ; 225 cmd->address = cpu_to_le32(address); 226 resp_len = sizeof(*resp); 227 228 ret = ath6kl_usb_ctrl_msg_exchange(ar_usb, 229 ATH6KL_USB_CONTROL_REQ_DIAG_CMD, 230 (u8 *) cmd, 231 sizeof(struct ath6kl_usb_ctrl_diag_cmd_write), 232 ATH6KL_USB_CONTROL_REQ_DIAG_RESP, 233 ar_usb->diag_resp_buffer, &resp_len); 234 235 if (ret) 236 return ret; 237 238 resp = (struct ath6kl_usb_ctrl_diag_resp_read *) 239 ar_usb->diag_resp_buffer; 240 241 *data = le32_to_cpu(resp->value); 242 243 return ret; 244 } 245 246 static int ath6kl_usb_diag_write32(struct ath6kl *ar, u32 address, __le32 data) 247 { 248 struct ath6kl_usb *ar_usb = ar->hif_priv; 249 struct ath6kl_usb_ctrl_diag_cmd_write *cmd; 250 251 cmd = (struct ath6kl_usb_ctrl_diag_cmd_write *) ar_usb->diag_cmd_buffer; 252 253 memset(cmd, 0, sizeof(struct ath6kl_usb_ctrl_diag_cmd_write)); 254 cmd->cmd = cpu_to_le32(ATH6KL_USB_CTRL_DIAG_CC_WRITE); 255 cmd->address = cpu_to_le32(address); 256 cmd->value = data; 257 258 return ath6kl_usb_ctrl_msg_exchange(ar_usb, 259 ATH6KL_USB_CONTROL_REQ_DIAG_CMD, 260 (u8 *) cmd, 261 sizeof(*cmd), 262 0, NULL, NULL); 263 264 } 265 266 static int ath6kl_usb_bmi_read(struct ath6kl *ar, u8 *buf, u32 len) 267 { 268 struct ath6kl_usb *ar_usb = ar->hif_priv; 269 int ret; 270 271 /* get response */ 272 ret = ath6kl_usb_submit_ctrl_in(ar_usb, 273 ATH6KL_USB_CONTROL_REQ_RECV_BMI_RESP, 274 0, 0, buf, len); 275 if (ret != 0) { 276 ath6kl_err("Unable to read the bmi data from the device: %d\n", 277 ret); 278 return ret; 279 } 280 281 return 0; 282 } 283 284 static int ath6kl_usb_bmi_write(struct ath6kl *ar, u8 *buf, u32 len) 285 { 286 struct ath6kl_usb *ar_usb = ar->hif_priv; 287 int ret; 288 289 /* send command */ 290 ret = ath6kl_usb_submit_ctrl_out(ar_usb, 291 ATH6KL_USB_CONTROL_REQ_SEND_BMI_CMD, 292 0, 0, buf, len); 293 if (ret != 0) { 294 ath6kl_err("unable to send the bmi data to the device: %d\n", 295 ret); 296 return ret; 297 } 298 299 return 0; 300 } 301 302 static int ath6kl_usb_power_on(struct ath6kl *ar) 303 { 304 return 0; 305 } 306 307 static int ath6kl_usb_power_off(struct ath6kl *ar) 308 { 309 return 0; 310 } 311 312 static const struct ath6kl_hif_ops ath6kl_usb_ops = { 313 .diag_read32 = ath6kl_usb_diag_read32, 314 .diag_write32 = ath6kl_usb_diag_write32, 315 .bmi_read = ath6kl_usb_bmi_read, 316 .bmi_write = ath6kl_usb_bmi_write, 317 .power_on = ath6kl_usb_power_on, 318 .power_off = ath6kl_usb_power_off, 319 }; 320 321 /* ath6kl usb driver registered functions */ 322 static int ath6kl_usb_probe(struct usb_interface *interface, 323 const struct usb_device_id *id) 324 { 325 struct usb_device *dev = interface_to_usbdev(interface); 326 struct ath6kl *ar; 327 struct ath6kl_usb *ar_usb = NULL; 328 int vendor_id, product_id; 329 int ret = 0; 330 331 usb_get_dev(dev); 332 333 vendor_id = le16_to_cpu(dev->descriptor.idVendor); 334 product_id = le16_to_cpu(dev->descriptor.idProduct); 335 336 ath6kl_dbg(ATH6KL_DBG_USB, "vendor_id = %04x\n", vendor_id); 337 ath6kl_dbg(ATH6KL_DBG_USB, "product_id = %04x\n", product_id); 338 339 if (interface->cur_altsetting) 340 ath6kl_dbg(ATH6KL_DBG_USB, "USB Interface %d\n", 341 interface->cur_altsetting->desc.bInterfaceNumber); 342 343 344 if (dev->speed == USB_SPEED_HIGH) 345 ath6kl_dbg(ATH6KL_DBG_USB, "USB 2.0 Host\n"); 346 else 347 ath6kl_dbg(ATH6KL_DBG_USB, "USB 1.1 Host\n"); 348 349 ar_usb = ath6kl_usb_create(interface); 350 351 if (ar_usb == NULL) { 352 ret = -ENOMEM; 353 goto err_usb_put; 354 } 355 356 ar = ath6kl_core_create(&ar_usb->udev->dev); 357 if (ar == NULL) { 358 ath6kl_err("Failed to alloc ath6kl core\n"); 359 ret = -ENOMEM; 360 goto err_usb_destroy; 361 } 362 363 ar->hif_priv = ar_usb; 364 ar->hif_type = ATH6KL_HIF_TYPE_USB; 365 ar->hif_ops = &ath6kl_usb_ops; 366 ar->mbox_info.block_size = 16; 367 ar->bmi.max_data_size = 252; 368 369 ar_usb->ar = ar; 370 371 ret = ath6kl_core_init(ar); 372 if (ret) { 373 ath6kl_err("Failed to init ath6kl core: %d\n", ret); 374 goto err_core_free; 375 } 376 377 return ret; 378 379 err_core_free: 380 ath6kl_core_destroy(ar); 381 err_usb_destroy: 382 ath6kl_usb_destroy(ar_usb); 383 err_usb_put: 384 usb_put_dev(dev); 385 386 return ret; 387 } 388 389 static void ath6kl_usb_remove(struct usb_interface *interface) 390 { 391 usb_put_dev(interface_to_usbdev(interface)); 392 ath6kl_usb_device_detached(interface); 393 } 394 395 /* table of devices that work with this driver */ 396 static struct usb_device_id ath6kl_usb_ids[] = { 397 {USB_DEVICE(0x0cf3, 0x9374)}, 398 { /* Terminating entry */ }, 399 }; 400 401 MODULE_DEVICE_TABLE(usb, ath6kl_usb_ids); 402 403 static struct usb_driver ath6kl_usb_driver = { 404 .name = "ath6kl_usb", 405 .probe = ath6kl_usb_probe, 406 .disconnect = ath6kl_usb_remove, 407 .id_table = ath6kl_usb_ids, 408 }; 409 410 static int ath6kl_usb_init(void) 411 { 412 usb_register(&ath6kl_usb_driver); 413 return 0; 414 } 415 416 static void ath6kl_usb_exit(void) 417 { 418 usb_deregister(&ath6kl_usb_driver); 419 } 420 421 module_init(ath6kl_usb_init); 422 module_exit(ath6kl_usb_exit); 423 424 MODULE_AUTHOR("Atheros Communications, Inc."); 425 MODULE_DESCRIPTION("Driver support for Atheros AR600x USB devices"); 426 MODULE_LICENSE("Dual BSD/GPL"); 427 MODULE_FIRMWARE(AR6004_HW_1_0_FIRMWARE_FILE); 428 MODULE_FIRMWARE(AR6004_HW_1_0_BOARD_DATA_FILE); 429 MODULE_FIRMWARE(AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE); 430 MODULE_FIRMWARE(AR6004_HW_1_1_FIRMWARE_FILE); 431 MODULE_FIRMWARE(AR6004_HW_1_1_BOARD_DATA_FILE); 432 MODULE_FIRMWARE(AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE); 433