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 unsigned int usb_dma_ctrl = 0;
188 /* Set PTA mode to USB */
189 andl(AR9170_PTA_REG_DMA_MODE_CTRL,
190 ~AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
192 /* Do a software reset to PTA component */
193 orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
194 andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
196 if (usb_detect_highspeed()) {
197 fw.usb.os_cfg_desc = &usb_config_fullspeed;
198 fw.usb.cfg_desc = &usb_config_highspeed;
200 /* 512 Byte DMA transfers */
201 usb_dma_ctrl |= AR9170_USB_DMA_CTL_HIGH_SPEED;
203 fw.usb.cfg_desc = &usb_config_fullspeed;
204 fw.usb.os_cfg_desc = &usb_config_highspeed;
207 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
208 # if (CONFIG_CARL9170FW_RX_FRAME_LEN == 4096)
209 usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_4K;
210 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 8192)
211 usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_8K;
212 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 16384)
213 usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_16K;
214 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 32768)
215 usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_32K;
217 # error "Invalid AR9170_RX_FRAME_LEN setting"
220 #else /* CONFIG_CARL9170FW_USB_UP_STREAM */
221 usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_PACKET_MODE;
222 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
224 #ifdef CONFIG_CARL9170FW_USB_DOWN_STREAM
225 /* Enable down stream mode */
226 usb_dma_ctrl |= AR9170_USB_DMA_CTL_DOWN_STREAM;
227 #endif /* CONFIG_CARL9170FW_USB_DOWN_STREAM */
229 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
230 /* Set the up stream mode maximum aggregate number */
231 set(AR9170_USB_REG_MAX_AGG_UPLOAD, 4);
234 * Set the up stream mode timeout value.
235 * NB: The vendor driver (otus) set 0x80?
237 set(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
238 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
240 /* Enable up stream and down stream */
241 usb_dma_ctrl |= AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE |
242 AR9170_USB_DMA_CTL_ENABLE_FROM_DEVICE;
244 set(AR9170_USB_REG_DMA_CTL, usb_dma_ctrl);
246 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
250 #ifdef CONFIG_CARL9170FW_USB_INIT_FIRMWARE
252 #endif /* CONFIG_CARL9170FW_USB_INIT_FIRMWARE */
256 * The fw structure is always initialized with "0"
257 * during boot(); No need to waste precious bytes here.
259 * fw.usb.interface_setting = 0;
260 * fw.usb.alternate_interface_setting = 0;
264 #define GET_ARRAY(a, o) ((uint32_t *) (((unsigned long) data) + offset))
266 static void usb_ep0rx_data(const void *data, const unsigned int len)
271 BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
272 BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
274 for (offset = 0; offset < ((len + 3) & ~3); offset += 4) {
275 value = get(AR9170_USB_REG_EP0_DATA);
276 memcpy(GET_ARRAY(data, offset), &value,
277 min(len - offset, (unsigned int)4));
281 static int usb_ep0tx_data(const void *data, const unsigned int len)
283 unsigned int offset = 0, block, last_block = 0;
286 BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
287 BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
289 block = min(len, (unsigned int) 4);
291 while (offset < len) {
293 if (last_block != block || block < 4)
294 setb(AR9170_USB_REG_FIFO_SIZE, (1 << block) - 1);
296 memcpy(&value, GET_ARRAY(data, offset), block);
298 set(AR9170_USB_REG_EP0_DATA, value);
301 last_block = block = min(len - offset, (unsigned int) 4);
304 setb(AR9170_USB_REG_FIFO_SIZE, 0xf);
306 /* this will push the data to the host */
311 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
312 static int usb_get_status(const struct usb_ctrlrequest *ctrl)
314 __le16 status = cpu_to_le16(0);
316 if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_IN)
319 switch (ctrl->bRequestType & USB_RECIP_MASK) {
320 case USB_RECIP_DEVICE:
321 status &= cpu_to_le16(~USB_DEVICE_SELF_POWERED);
322 status &= cpu_to_le16(~USB_DEVICE_REMOTE_WAKEUP);
325 case USB_RECIP_INTERFACE:
326 /* USB spec: This is reserved for future use. */
327 status = cpu_to_le16(0);
330 case USB_RECIP_ENDPOINT:
331 case USB_RECIP_OTHER:
336 return usb_ep0tx_data((const void *) &status, sizeof(status));
339 static int usb_get_string_desc(const struct usb_ctrlrequest *ctrl)
341 const struct usb_string_descriptor *string_desc = NULL;
343 switch (le16_to_cpu(ctrl->wValue) & 0xff) {
345 string_desc = (const struct usb_string_descriptor *)
346 rom.hw.usb.string0_desc;
350 string_desc = (const struct usb_string_descriptor *)
351 rom.hw.usb.string1_desc;
355 string_desc = (const struct usb_string_descriptor *)
356 rom.hw.usb.string2_desc;
360 string_desc = (const struct usb_string_descriptor *)
361 rom.hw.usb.string3_desc;
369 return usb_ep0tx_data(string_desc, string_desc->bLength);
374 static int usb_get_device_desc(const struct usb_ctrlrequest *ctrl __unused)
376 return usb_ep0tx_data(&rom.hw.usb.device_desc,
377 rom.hw.usb.device_desc.bLength);
380 static int usb_get_config_desc(const struct usb_ctrlrequest *ctrl __unused)
382 fw.usb.cfg_desc->cfg.bDescriptorType = USB_DT_CONFIG;
384 return usb_ep0tx_data(fw.usb.cfg_desc,
385 le16_to_cpu(fw.usb.cfg_desc->cfg.wTotalLength));
388 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
389 static int usb_get_otherspeed_desc(const struct usb_ctrlrequest *ctrl __unused)
392 fw.usb.os_cfg_desc->cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
394 return usb_ep0tx_data(fw.usb.os_cfg_desc,
395 le16_to_cpu(fw.usb.os_cfg_desc->cfg.wTotalLength));
397 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
399 static int usb_get_qualifier_desc(const struct usb_ctrlrequest *ctrl __unused)
401 struct usb_qualifier_descriptor qual;
404 * The qualifier descriptor shares some structural details
405 * with the main device descriptor.
408 memcpy(&qual, &rom.hw.usb.device_desc, sizeof(qual));
410 /* (Re)-Initialize fields */
411 qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
412 qual.bLength = sizeof(qual);
413 qual.bNumConfigurations = rom.hw.usb.device_desc.bNumConfigurations;
416 return usb_ep0tx_data(&qual, qual.bLength);
419 #define USB_CHECK_REQTYPE(ctrl, recip, dir) \
420 (((ctrl->bRequestType & USB_RECIP_MASK) != recip) || \
421 ((ctrl->bRequestType & USB_DIR_MASK) != dir))
423 static int usb_get_descriptor(const struct usb_ctrlrequest *ctrl)
427 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
430 switch (le16_to_cpu(ctrl->wValue) >> 8) {
432 status = usb_get_device_desc(ctrl);
436 status = usb_get_config_desc(ctrl);
440 status = usb_get_string_desc(ctrl);
443 case USB_DT_INTERFACE:
446 case USB_DT_ENDPOINT:
449 case USB_DT_DEVICE_QUALIFIER:
450 status = usb_get_qualifier_desc(ctrl);
453 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
454 case USB_DT_OTHER_SPEED_CONFIG:
455 status = usb_get_otherspeed_desc(ctrl);
457 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
466 static int usb_get_configuration(const struct usb_ctrlrequest *ctrl)
468 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
471 return usb_ep0tx_data(&fw.usb.config, 1);
474 static int usb_set_configuration(const struct usb_ctrlrequest *ctrl)
478 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
481 config = le16_to_cpu(ctrl->wValue);
485 andb(AR9170_USB_REG_DEVICE_ADDRESS,
486 (uint8_t) ~(AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
487 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
489 fw.usb.config = config;
491 if (usb_detect_highspeed()) {
492 /* High Speed Configuration */
493 usb_init_highspeed_fifo_cfg();
495 /* Full Speed Configuration */
496 usb_init_fullspeed_fifo_cfg();
503 /* usb_pta_init() ? */
506 orb(AR9170_USB_REG_DEVICE_ADDRESS,
507 (AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
509 usb_enable_global_int();
516 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
519 static int usb_set_address(const struct usb_ctrlrequest *ctrl)
521 unsigned int address;
523 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
526 address = le16_to_cpu(ctrl->wValue);
529 * The original firmware used 0x100 (which is, of course,
530 * too big to fit into uint8_t).
531 * However based on the available information (hw.h), BIT(7)
532 * is used as some sort of flag and should not be
533 * part of the device address.
535 if (address >= BIT(7))
538 setb(AR9170_USB_REG_DEVICE_ADDRESS, (uint8_t) address);
542 static int usb_get_interface(const struct usb_ctrlrequest *ctrl)
544 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_IN))
547 if (usb_configured() == false)
550 switch (fw.usb.config) {
558 return usb_ep0tx_data(&fw.usb.alternate_interface_setting, 1);
561 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
562 static int usb_set_interface(const struct usb_ctrlrequest *ctrl)
564 unsigned int intf, alt_intf;
565 if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_OUT))
568 if (usb_configured() == false)
571 intf = le16_to_cpu(ctrl->wIndex);
572 alt_intf = le16_to_cpu(ctrl->wValue);
576 if (alt_intf != fw.usb.cfg_desc->intf.bAlternateSetting)
579 fw.usb.interface_setting = (uint8_t) intf;
580 fw.usb.alternate_interface_setting = (uint8_t) alt_intf;
581 if (usb_detect_highspeed())
582 usb_init_highspeed_fifo_cfg();
584 usb_init_fullspeed_fifo_cfg();
587 usb_enable_global_int();
595 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
596 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
598 static int usb_standard_command(const struct usb_ctrlrequest *ctrl __unused)
602 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
603 switch (ctrl->bRequest) {
604 case USB_REQ_GET_STATUS:
605 status = usb_get_status(ctrl);
608 case USB_REQ_CLEAR_FEATURE:
611 case USB_REQ_SET_FEATURE:
614 case USB_REQ_SET_ADDRESS:
615 status = usb_set_address(ctrl);
618 case USB_REQ_GET_DESCRIPTOR:
619 status = usb_get_descriptor(ctrl);
622 case USB_REQ_SET_DESCRIPTOR:
625 case USB_REQ_GET_CONFIGURATION:
626 status = usb_get_configuration(ctrl);
629 case USB_REQ_SET_CONFIGURATION:
630 status = usb_set_configuration(ctrl);
633 case USB_REQ_GET_INTERFACE:
634 status = usb_get_interface(ctrl);
637 case USB_REQ_SET_INTERFACE:
638 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
639 status = usb_set_interface(ctrl);
640 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
643 case USB_REQ_SYNCH_FRAME:
650 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
655 static int usb_class_command(const struct usb_ctrlrequest *ctrl __unused)
660 static int usb_vendor_command(const struct usb_ctrlrequest *ctrl __unused)
663 * Note: Firmware upload/boot is not implemented.
664 * It's impossible to replace the current image
671 #undef USB_CHECK_TYPE
673 void usb_ep0setup(void)
675 struct usb_ctrlrequest ctrl;
677 usb_ep0rx_data(&ctrl, sizeof(ctrl));
679 switch (ctrl.bRequestType & USB_TYPE_MASK) {
680 case USB_TYPE_STANDARD:
681 status = usb_standard_command(&ctrl);
685 status = usb_class_command(&ctrl);
688 case USB_TYPE_VENDOR:
689 status = usb_vendor_command(&ctrl);
698 fw.usb.ep0_action |= CARL9170_EP0_STALL;
700 fw.usb.ep0_action |= CARL9170_EP0_TRIGGER;
705 if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
708 usb_ep0rx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
709 fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
714 if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
717 usb_ep0tx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
718 fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;