2 * carl9170 firmware - used by the ar9170 wireless device
6 * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7 * Copyright (c) 2007-2009 Atheros Communications, Inc.
8 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
9 * Copyright 2009 Christian Lamparter <chunkeey@googlemail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (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 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 * NB: The firmware has to write into these structures
32 * so don't try to make them "const".
35 static struct ar9170_usb_config usb_config_highspeed = {
37 .bLength = USB_DT_CONFIG_SIZE,
38 .bDescriptorType = USB_DT_CONFIG,
39 .wTotalLength = cpu_to_le16(sizeof(usb_config_highspeed)),
41 .bConfigurationValue = 1,
43 .bmAttributes = USB_CONFIG_ATT_ONE,
44 .bMaxPower = 0xfa, /* 500 mA */
48 .bLength = USB_DT_INTERFACE_SIZE,
49 .bDescriptorType = USB_DT_INTERFACE,
50 .bInterfaceNumber = 0,
51 .bAlternateSetting = 0,
52 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
53 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
54 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
55 .bInterfaceProtocol = 0,
61 .bLength = USB_DT_ENDPOINT_SIZE,
62 .bDescriptorType = USB_DT_ENDPOINT,
63 .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
64 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 .wMaxPacketSize = cpu_to_le16(512),
70 .bLength = USB_DT_ENDPOINT_SIZE,
71 .bDescriptorType = USB_DT_ENDPOINT,
72 .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74 .wMaxPacketSize = cpu_to_le16(512),
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
82 .bmAttributes = USB_ENDPOINT_XFER_INT,
83 .wMaxPacketSize = cpu_to_le16(64),
88 .bLength = USB_DT_ENDPOINT_SIZE,
89 .bDescriptorType = USB_DT_ENDPOINT,
90 .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
91 .bmAttributes = USB_ENDPOINT_XFER_INT,
92 .wMaxPacketSize = cpu_to_le16(64),
98 static struct ar9170_usb_config usb_config_fullspeed = {
100 .bLength = USB_DT_CONFIG_SIZE,
101 .bDescriptorType = USB_DT_CONFIG,
102 .wTotalLength = cpu_to_le16(sizeof(usb_config_fullspeed)),
104 .bConfigurationValue = 1,
106 .bmAttributes = USB_CONFIG_ATT_ONE,
107 .bMaxPower = 0xfa, /* 500 mA */
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 .bInterfaceNumber = 0,
114 .bAlternateSetting = 0,
115 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
116 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
117 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
118 .bInterfaceProtocol = 0,
124 .bLength = USB_DT_ENDPOINT_SIZE,
125 .bDescriptorType = USB_DT_ENDPOINT,
126 .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
127 .bmAttributes = USB_ENDPOINT_XFER_BULK,
128 .wMaxPacketSize = cpu_to_le16(64),
133 .bLength = USB_DT_ENDPOINT_SIZE,
134 .bDescriptorType = USB_DT_ENDPOINT,
135 .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
136 .bmAttributes = USB_ENDPOINT_XFER_BULK,
137 .wMaxPacketSize = cpu_to_le16(64),
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144 .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
145 .bmAttributes = USB_ENDPOINT_XFER_INT,
146 .wMaxPacketSize = cpu_to_le16(64),
151 .bLength = USB_DT_ENDPOINT_SIZE,
152 .bDescriptorType = USB_DT_ENDPOINT,
153 .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
154 .bmAttributes = USB_ENDPOINT_XFER_INT,
155 .wMaxPacketSize = cpu_to_le16(64),
161 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
162 static void usb_reset_eps(void)
166 /* clear all EPs' toggle bit */
167 for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
168 usb_set_input_ep_toggle(i);
169 usb_clear_input_ep_toggle(i);
173 * NB: I've no idea why this cannot be integrated into the
176 for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
177 usb_set_output_ep_toggle(i);
178 usb_clear_output_ep_toggle(i);
181 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
184 #ifdef CONFIG_CARL9170FW_USB_INIT_FIRMWARE
185 static void usb_pta_init(void)
187 /* Set PTA mode to USB */
188 andl(AR9170_PTA_REG_DMA_MODE_CTRL,
189 ~AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
191 /* Do a software reset to PTA component */
192 orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
193 andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
195 if (usb_detect_highspeed()) {
196 fw.usb.os_cfg_desc = &usb_config_fullspeed;
197 fw.usb.cfg_desc = &usb_config_highspeed;
199 /* 512 Byte DMA transfers */
200 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_HIGH_SPEED);
202 fw.usb.cfg_desc = &usb_config_fullspeed;
203 fw.usb.os_cfg_desc = &usb_config_highspeed;
206 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
207 /* Enable upload stream mode */
208 andl(AR9170_USB_REG_DMA_CTL, ~AR9170_DMA_CTL_UP_PACKET_MODE);
210 /* reset maximum transfer size */
211 andl(AR9170_USB_REG_DMA_CTL, ~(AR9170_DMA_CTL_UP_STREAM));
213 # if (CONFIG_CARL9170FW_RX_FRAME_LEN == 4096)
214 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_UP_STREAM_4K);
215 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 8192)
216 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_UP_STREAM_8K);
217 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 16384)
218 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_UP_STREAM_16K);
219 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 32768)
220 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_UP_STREAM_32K);
222 # error "Invalid AR9170_RX_FRAME_LEN setting"
225 #else /* CONFIG_CARL9170FW_USB_UP_STREAM */
226 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_UP_PACKET_MODE);
227 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
229 #ifdef CONFIG_CARL9170FW_USB_DOWN_STREAM
230 /* Enable down stream mode */
231 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_DOWN_STREAM);
232 #endif /* CONFIG_CARL9170FW_USB_DOWN_STREAM */
234 /* Enable up stream and down stream */
235 orl(AR9170_USB_REG_DMA_CTL, AR9170_DMA_CTL_ENABLE_TO_DEVICE |
236 AR9170_DMA_CTL_ENABLE_FROM_DEVICE);
238 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
239 /* Set the up stream mode maximum aggregate number */
240 set(AR9170_USB_REG_MAX_AGG_UPLOAD, 4);
243 * Set the up stream mode timeout value.
244 * NB: The vendor driver (otus) set 0x80?
246 set(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
247 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
250 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
254 #ifdef CONFIG_CARL9170FW_USB_INIT_FIRMWARE
256 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
260 * The fw structure is always initialized with "0"
261 * during boot(); No need to waste precious bytes here.
263 * fw.usb.interface_setting = 0;
264 * fw.usb.alternate_interface_setting = 0;
268 #define GET_ARRAY(a, o) ((uint32_t *) (((unsigned long) data) + offset))
270 static void usb_ep0rx_data(const void *data, const unsigned int len)
275 BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
276 BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
278 for (offset = 0; offset < ((len + 3) & ~3); offset += 4) {
279 value = get(AR9170_USB_REG_EP0_DATA);
280 memcpy(GET_ARRAY(data, offset), &value,
281 min(len - offset, (unsigned int)4));
285 static int usb_ep0tx_data(const void *data, const unsigned int len)
287 unsigned int offset = 0, block, last_block = 0;
290 BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
291 BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
293 block = min(len, (unsigned int) 4);
295 while (offset < len) {
297 if (last_block != block || block < 4)
298 setb(AR9170_USB_REG_FIFO_SIZE, (1 << block) - 1);
300 memcpy(&value, GET_ARRAY(data, offset), block);
302 set(AR9170_USB_REG_EP0_DATA, value);
305 last_block = block = min(len - offset, (unsigned int) 4);
308 setb(AR9170_USB_REG_FIFO_SIZE, 0xf);
310 /* this will push the data to the host */
315 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
316 static int usb_get_status(const struct usb_ctrlrequest *ctrl)
318 __le16 status = cpu_to_le16(0);
320 if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_IN)
323 switch (ctrl->bRequestType & USB_RECIP_MASK) {
324 case USB_RECIP_DEVICE:
325 status &= cpu_to_le16(~USB_DEVICE_SELF_POWERED);
326 status &= cpu_to_le16(~USB_DEVICE_REMOTE_WAKEUP);
329 case USB_RECIP_INTERFACE:
330 /* USB spec: This is reserved for future use. */
331 status = cpu_to_le16(0);
334 case USB_RECIP_ENDPOINT:
335 case USB_RECIP_OTHER:
340 return usb_ep0tx_data((const void *) &status, sizeof(status));
343 static int usb_get_string_desc(const struct usb_ctrlrequest *ctrl)
345 const struct usb_string_descriptor *string_desc = NULL;
347 switch (le16_to_cpu(ctrl->wValue) & 0xff) {
349 string_desc = (const struct usb_string_descriptor *)
350 rom.hw.usb.string0_desc;
354 string_desc = (const struct usb_string_descriptor *)
355 rom.hw.usb.string1_desc;
359 string_desc = (const struct usb_string_descriptor *)
360 rom.hw.usb.string2_desc;
364 string_desc = (const struct usb_string_descriptor *)
365 rom.hw.usb.string3_desc;
373 return usb_ep0tx_data(string_desc, string_desc->bLength);
378 static int usb_get_device_desc(const struct usb_ctrlrequest *ctrl __unused)
380 return usb_ep0tx_data(&rom.hw.usb.device_desc,
381 rom.hw.usb.device_desc.bLength);
384 static int usb_get_config_desc(const struct usb_ctrlrequest *ctrl __unused)
386 fw.usb.cfg_desc->cfg.bDescriptorType = USB_DT_CONFIG;
388 return usb_ep0tx_data(fw.usb.cfg_desc,
389 le16_to_cpu(fw.usb.cfg_desc->cfg.wTotalLength));
392 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
393 static int usb_get_otherspeed_desc(const struct usb_ctrlrequest *ctrl __unused)
396 fw.usb.os_cfg_desc->cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
398 return usb_ep0tx_data(fw.usb.os_cfg_desc,
399 le16_to_cpu(fw.usb.os_cfg_desc->cfg.wTotalLength));
401 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
403 static int usb_get_qualifier_desc(const struct usb_ctrlrequest *ctrl __unused)
405 struct usb_qualifier_descriptor qual;
408 * The qualifier descriptor shares some structural details
409 * with the main device descriptor.
412 memcpy(&qual, &rom.hw.usb.device_desc, sizeof(qual));
414 /* (Re)-Initialize fields */
415 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
416 qual.bLength = sizeof(qual);
417 qual.bNumConfigurations = rom.hw.usb.device_desc.bNumConfigurations;
420 return usb_ep0tx_data(&qual, qual.bLength);
423 #define USB_CHECK_REQTYPE(ctrl, recip, dir) \
424 (((ctrl->bRequestType & USB_RECIP_MASK) != recip) || \
425 ((ctrl->bRequestType & USB_DIR_MASK) != dir))
427 static int usb_get_descriptor(const struct usb_ctrlrequest *ctrl)
431 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
434 switch (le16_to_cpu(ctrl->wValue) >> 8) {
436 status = usb_get_device_desc(ctrl);
440 status = usb_get_config_desc(ctrl);
444 status = usb_get_string_desc(ctrl);
447 case USB_DT_INTERFACE:
450 case USB_DT_ENDPOINT:
453 case USB_DT_DEVICE_QUALIFIER:
454 status = usb_get_qualifier_desc(ctrl);
457 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
458 case USB_DT_OTHER_SPEED_CONFIG:
459 status = usb_get_otherspeed_desc(ctrl);
461 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
470 static int usb_get_configuration(const struct usb_ctrlrequest *ctrl)
472 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
475 return usb_ep0tx_data(&fw.usb.config, 1);
478 static int usb_set_configuration(const struct usb_ctrlrequest *ctrl)
482 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
485 config = le16_to_cpu(ctrl->wValue);
489 andb(AR9170_USB_REG_DEVICE_ADDRESS,
490 (uint8_t) ~(AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
491 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
493 fw.usb.config = config;
495 if (usb_detect_highspeed()) {
496 /* High Speed Configuration */
497 usb_init_highspeed_fifo_cfg();
499 /* Full Speed Configuration */
500 usb_init_fullspeed_fifo_cfg();
507 /* usb_pta_init() ? */
510 orb(AR9170_USB_REG_DEVICE_ADDRESS,
511 (AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
513 usb_enable_global_int();
520 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
523 static int usb_set_address(const struct usb_ctrlrequest *ctrl)
525 unsigned int address;
527 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
530 address = le16_to_cpu(ctrl->wValue);
533 * The original firmware used 0x100 (which is, of course,
534 * too big to fit into uint8_t).
535 * However based on the available information (hw.h), BIT(7)
536 * is used as some sort of flag and should not be
537 * part of the device address.
539 if (address >= BIT(7))
542 setb(AR9170_USB_REG_DEVICE_ADDRESS, (uint8_t) address);
546 static int usb_get_interface(const struct usb_ctrlrequest *ctrl)
548 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_IN))
551 if (usb_configured() == false)
554 switch (fw.usb.config) {
562 return usb_ep0tx_data(&fw.usb.alternate_interface_setting, 1);
565 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
566 static int usb_set_interface(const struct usb_ctrlrequest *ctrl)
568 unsigned int intf, alt_intf;
569 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_OUT))
572 if (usb_configured() == false)
575 intf = le16_to_cpu(ctrl->wIndex);
576 alt_intf = le16_to_cpu(ctrl->wValue);
580 if (alt_intf != fw.usb.cfg_desc->intf.bAlternateSetting)
583 fw.usb.interface_setting = (uint8_t) intf;
584 fw.usb.alternate_interface_setting = (uint8_t) alt_intf;
585 if (usb_detect_highspeed())
586 usb_init_highspeed_fifo_cfg();
588 usb_init_fullspeed_fifo_cfg();
591 usb_enable_global_int();
599 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
600 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
602 static int usb_standard_command(const struct usb_ctrlrequest *ctrl __unused)
606 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
607 switch (ctrl->bRequest) {
608 case USB_REQ_GET_STATUS:
609 status = usb_get_status(ctrl);
612 case USB_REQ_CLEAR_FEATURE:
615 case USB_REQ_SET_FEATURE:
618 case USB_REQ_SET_ADDRESS:
619 status = usb_set_address(ctrl);
622 case USB_REQ_GET_DESCRIPTOR:
623 status = usb_get_descriptor(ctrl);
626 case USB_REQ_SET_DESCRIPTOR:
629 case USB_REQ_GET_CONFIGURATION:
630 status = usb_get_configuration(ctrl);
633 case USB_REQ_SET_CONFIGURATION:
634 status = usb_set_configuration(ctrl);
637 case USB_REQ_GET_INTERFACE:
638 status = usb_get_interface(ctrl);
641 case USB_REQ_SET_INTERFACE:
642 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
643 status = usb_set_interface(ctrl);
644 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
647 case USB_REQ_SYNCH_FRAME:
654 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
659 static int usb_class_command(const struct usb_ctrlrequest *ctrl __unused)
664 static int usb_vendor_command(const struct usb_ctrlrequest *ctrl __unused)
667 * Note: Firmware upload/boot is not implemented.
668 * It's impossible to replace the current image
675 #undef USB_CHECK_TYPE
677 void usb_ep0setup(void)
679 struct usb_ctrlrequest ctrl;
681 usb_ep0rx_data(&ctrl, sizeof(ctrl));
683 switch (ctrl.bRequestType & USB_TYPE_MASK) {
684 case USB_TYPE_STANDARD:
685 status = usb_standard_command(&ctrl);
689 status = usb_class_command(&ctrl);
692 case USB_TYPE_VENDOR:
693 status = usb_vendor_command(&ctrl);
702 fw.usb.ep0_action |= CARL9170_EP0_STALL;
704 fw.usb.ep0_action |= CARL9170_EP0_TRIGGER;
709 if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
712 usb_ep0rx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
713 fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
718 if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
721 usb_ep0tx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
722 fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;