carl9170 firmware: implement endpoint halt req/clear and get endpoint status
[carl9170fw.git] / carlfw / usb / usb.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * USB Controller
5  *
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-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
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.
15  *
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.
20  *
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.
24  */
25 #include "carl9170.h"
26 #include "usb.h"
27 #include "printf.h"
28 #include "rom.h"
29
30 /*
31  * NB: The firmware has to write into these structures
32  * so don't try to make them "const".
33  */
34
35 static struct ar9170_usb_config usb_config_highspeed = {
36         .cfg = {
37                 .bLength = USB_DT_CONFIG_SIZE,
38                 .bDescriptorType = USB_DT_CONFIG,
39                 .wTotalLength = cpu_to_le16(sizeof(usb_config_highspeed)),
40                 .bNumInterfaces = 1,
41                 .bConfigurationValue = 1,
42                 .iConfiguration = 0,
43                 .bmAttributes = USB_CONFIG_ATT_ONE |
44 #ifdef CONFIG_CARL9170FW_WOL
45                                 USB_CONFIG_ATT_WAKEUP |
46 #endif /* CONFIG_CARL9170FW_WOL */
47                                 0,
48                 .bMaxPower = 0xfa, /* 500 mA */
49         },
50
51         .intf = {
52                 .bLength = USB_DT_INTERFACE_SIZE,
53                 .bDescriptorType = USB_DT_INTERFACE,
54                 .bInterfaceNumber = 0,
55                 .bAlternateSetting = 0,
56                 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
57                 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
58                 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
59                 .bInterfaceProtocol = 0,
60                 .iInterface = 0,
61         },
62
63         .ep = {
64                 {       /* EP 1 */
65                         .bLength = USB_DT_ENDPOINT_SIZE,
66                         .bDescriptorType = USB_DT_ENDPOINT,
67                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
68                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
69                         .wMaxPacketSize = cpu_to_le16(512),
70                         .bInterval = 0,
71                 },
72
73                 {       /* EP 2 */
74                         .bLength = USB_DT_ENDPOINT_SIZE,
75                         .bDescriptorType = USB_DT_ENDPOINT,
76                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
77                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
78                         .wMaxPacketSize = cpu_to_le16(512),
79                         .bInterval = 0,
80                 },
81
82                 {       /* EP 3 */
83                         .bLength = USB_DT_ENDPOINT_SIZE,
84                         .bDescriptorType = USB_DT_ENDPOINT,
85                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
86                         .bmAttributes = USB_ENDPOINT_XFER_INT,
87                         .wMaxPacketSize = cpu_to_le16(64),
88                         .bInterval = 1,
89                 },
90
91                 {       /* EP 4 */
92                         .bLength = USB_DT_ENDPOINT_SIZE,
93                         .bDescriptorType = USB_DT_ENDPOINT,
94                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
95                         .bmAttributes = USB_ENDPOINT_XFER_INT,
96                         .wMaxPacketSize = cpu_to_le16(64),
97                         .bInterval = 1,
98                 },
99         },
100 };
101
102 static struct ar9170_usb_config usb_config_fullspeed = {
103         .cfg = {
104                 .bLength = USB_DT_CONFIG_SIZE,
105                 .bDescriptorType = USB_DT_CONFIG,
106                 .wTotalLength = cpu_to_le16(sizeof(usb_config_fullspeed)),
107                 .bNumInterfaces = 1,
108                 .bConfigurationValue = 1,
109                 .iConfiguration = 0,
110                 .bmAttributes = USB_CONFIG_ATT_ONE |
111 #ifdef CONFIG_CARL9170FW_WOL
112                                 USB_CONFIG_ATT_WAKEUP |
113 #endif /* CONFIG_CARL9170FW_WOL */
114                                 0,
115                 .bMaxPower = 0xfa, /* 500 mA */
116         },
117
118         .intf = {
119                 .bLength = USB_DT_INTERFACE_SIZE,
120                 .bDescriptorType = USB_DT_INTERFACE,
121                 .bInterfaceNumber = 0,
122                 .bAlternateSetting = 0,
123                 .bNumEndpoints = AR9170_USB_NUM_EXTRA_EP,
124                 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
125                 .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
126                 .bInterfaceProtocol = 0,
127                 .iInterface = 0,
128         },
129
130         .ep = {
131                 {       /* EP 1 */
132                         .bLength = USB_DT_ENDPOINT_SIZE,
133                         .bDescriptorType = USB_DT_ENDPOINT,
134                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_TX,
135                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
136                         .wMaxPacketSize = cpu_to_le16(64),
137                         .bInterval = 0,
138                 },
139
140                 {       /* EP 2 */
141                         .bLength = USB_DT_ENDPOINT_SIZE,
142                         .bDescriptorType = USB_DT_ENDPOINT,
143                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_RX,
144                         .bmAttributes = USB_ENDPOINT_XFER_BULK,
145                         .wMaxPacketSize = cpu_to_le16(64),
146                         .bInterval = 0,
147                 },
148
149                 {       /* EP 3 */
150                         .bLength = USB_DT_ENDPOINT_SIZE,
151                         .bDescriptorType = USB_DT_ENDPOINT,
152                         .bEndpointAddress = USB_DIR_IN | AR9170_USB_EP_IRQ,
153                         .bmAttributes = USB_ENDPOINT_XFER_INT,
154                         .wMaxPacketSize = cpu_to_le16(64),
155                         .bInterval = 1,
156                 },
157
158                 {       /* EP 4 */
159                         .bLength = USB_DT_ENDPOINT_SIZE,
160                         .bDescriptorType = USB_DT_ENDPOINT,
161                         .bEndpointAddress = USB_DIR_OUT | AR9170_USB_EP_CMD,
162                         .bmAttributes = USB_ENDPOINT_XFER_INT,
163                         .wMaxPacketSize = cpu_to_le16(64),
164                         .bInterval = 1,
165                 },
166         },
167 };
168
169 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
170 static void usb_reset_eps(void)
171 {
172         unsigned int i;
173
174         /* clear all EPs' toggle bit */
175         for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
176                 usb_set_input_ep_toggle(i);
177                 usb_clear_input_ep_toggle(i);
178                 usb_clear_input_ep_stall(i);
179         }
180
181         /*
182          * NB: I've no idea why this cannot be integrated into the
183          * previous loop?
184          */
185         for (i = 1; i < __AR9170_USB_NUM_MAX_EP; i++) {
186                 usb_set_output_ep_toggle(i);
187                 usb_clear_output_ep_toggle(i);
188                 usb_clear_output_ep_stall(i);
189         }
190 }
191 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
192
193
194 static void usb_pta_init(void)
195 {
196         unsigned int usb_dma_ctrl = 0;
197         /* Set PTA mode to USB */
198         andl(AR9170_PTA_REG_DMA_MODE_CTRL,
199                 ~AR9170_PTA_DMA_MODE_CTRL_DISABLE_USB);
200
201         /* Do a software reset to PTA component */
202         orl(AR9170_PTA_REG_DMA_MODE_CTRL, AR9170_PTA_DMA_MODE_CTRL_RESET);
203         andl(AR9170_PTA_REG_DMA_MODE_CTRL, ~AR9170_PTA_DMA_MODE_CTRL_RESET);
204
205         if (usb_detect_highspeed()) {
206                 fw.usb.os_cfg_desc = &usb_config_fullspeed;
207                 fw.usb.cfg_desc = &usb_config_highspeed;
208
209                 /* 512 Byte DMA transfers */
210                 usb_dma_ctrl |= AR9170_USB_DMA_CTL_HIGH_SPEED;
211         } else {
212                 fw.usb.cfg_desc = &usb_config_fullspeed;
213                 fw.usb.os_cfg_desc = &usb_config_highspeed;
214         }
215
216 #ifdef CONFIG_CARL9170FW_USB_UP_STREAM
217 # if (CONFIG_CARL9170FW_RX_FRAME_LEN == 4096)
218         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_4K;
219 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 8192)
220         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_8K;
221 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 16384)
222         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_16K;
223 # elif (CONFIG_CARL9170FW_RX_FRAME_LEN == 32768)
224         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_STREAM_32K;
225 # else
226 #       error "Invalid AR9170_RX_FRAME_LEN setting"
227 # endif
228
229 #else /* CONFIG_CARL9170FW_USB_UP_STREAM */
230         usb_dma_ctrl |= AR9170_USB_DMA_CTL_UP_PACKET_MODE;
231 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
232
233 #ifdef CONFIG_CARL9170FW_USB_DOWN_STREAM
234         /* Enable down stream mode */
235         usb_dma_ctrl |= AR9170_USB_DMA_CTL_DOWN_STREAM;
236 #endif /* CONFIG_CARL9170FW_USB_DOWN_STREAM */
237
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);
241
242         /*
243          * Set the up stream mode timeout value.
244          * NB: The vendor driver (otus) set 0x80?
245          */
246         set(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
247 #endif /* CONFIG_CARL9170FW_USB_UP_STREAM */
248
249         /* Enable up stream and down stream */
250         usb_dma_ctrl |= AR9170_USB_DMA_CTL_ENABLE_TO_DEVICE |
251             AR9170_USB_DMA_CTL_ENABLE_FROM_DEVICE;
252
253         set(AR9170_USB_REG_DMA_CTL, usb_dma_ctrl);
254 }
255
256 void usb_init(void)
257 {
258         usb_pta_init();
259
260         fw.usb.config = 1;
261         /*
262          * The fw structure is always initialized with "0"
263          * during boot(); No need to waste precious bytes here.
264          *
265          * fw.usb.interface_setting = 0;
266          * fw.usb.alternate_interface_setting = 0;
267          * fw.usb.device_feature = 0;
268          */
269
270 #ifdef CONFIG_CARL9170FW_WOL
271         fw.usb.device_feature |= USB_DEVICE_REMOTE_WAKEUP;
272         usb_enable_remote_wakeup();
273 #endif /* CONFIG_CARL9170FW_WOL */
274 }
275
276 #define GET_ARRAY(a, o) ((uint32_t *) (((unsigned long) data) + offset))
277
278 static void usb_ep0rx_data(const void *data, const unsigned int len)
279 {
280         unsigned int offset;
281         uint32_t value;
282
283         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
284         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
285
286         for (offset = 0; offset < ((len + 3) & ~3); offset += 4) {
287                 value = get(AR9170_USB_REG_EP0_DATA);
288                 memcpy(GET_ARRAY(data, offset), &value,
289                        min(len - offset, (unsigned int)4));
290         }
291 }
292
293 static int usb_ep0tx_data(const void *data, const unsigned int len)
294 {
295         unsigned int offset = 0, block, last_block = 0;
296         uint32_t value;
297
298         BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
299         BUILD_BUG_ON(len > AR9170_USB_EP_CTRL_MAX);
300
301         block = min(len, (unsigned int) 4);
302         offset = 0;
303         while (offset < len) {
304
305                 if (last_block != block || block < 4)
306                         setb(AR9170_USB_REG_FIFO_SIZE, (1 << block) - 1);
307
308                 memcpy(&value, GET_ARRAY(data, offset), block);
309
310                 set(AR9170_USB_REG_EP0_DATA, value);
311
312                 offset += block;
313                 last_block = block = min(len - offset, (unsigned int) 4);
314         }
315
316         setb(AR9170_USB_REG_FIFO_SIZE, 0xf);
317
318         /* this will push the data to the host */
319         return 1;
320 }
321 #undef GET_ARRAY
322
323 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
324 static int usb_get_status(const struct usb_ctrlrequest *ctrl)
325 {
326         __le16 status = cpu_to_le16(fw.usb.device_feature);
327
328         if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_IN)
329                 return -1;
330
331         switch (ctrl->bRequestType & USB_RECIP_MASK) {
332         case USB_RECIP_DEVICE:
333                 status &= cpu_to_le16(~USB_DEVICE_SELF_POWERED);
334                 status &= cpu_to_le16(~USB_DEVICE_REMOTE_WAKEUP);
335                 break;
336
337         case USB_RECIP_INTERFACE:
338                 /* USB spec: This is reserved for future use. */
339                 status = cpu_to_le16(0);
340                 break;
341
342         case USB_RECIP_ENDPOINT: {
343                 unsigned int ep = le16_to_cpu(ctrl->wIndex) & 0xf;
344                 unsigned int dir = le16_to_cpu(ctrl->wIndex) & USB_DIR_MASK;
345
346                 if (ep == 0) {
347                         status = !!(getb(AR9170_USB_REG_CX_CONFIG_STATUS) & BIT(2));
348                 } else {
349                         unsigned int addr;
350
351                         if (dir == USB_DIR_IN)
352                                 addr = AR9170_USB_REG_EP_IN_MAX_SIZE_HIGH;
353                         else
354                                 addr = AR9170_USB_REG_EP_OUT_MAX_SIZE_HIGH;
355
356                         addr += (ep << 1);
357
358                         /*
359                          * AR9170_USB_EP_OUT_STALL == AR9170_USB_EP_IN_STALL
360                          * so it doesn't matter which one we use
361                          */
362                         status = !!(getb(addr) & AR9170_USB_EP_OUT_STALL);
363                 }
364                 break;
365                 }
366         case USB_RECIP_OTHER:
367         default:
368                 break;
369         }
370
371         return usb_ep0tx_data((const void *) &status, sizeof(status));
372 }
373
374 static int usb_get_string_desc(const struct usb_ctrlrequest *ctrl)
375 {
376         const struct usb_string_descriptor *string_desc = NULL;
377
378         switch (le16_to_cpu(ctrl->wValue) & 0xff) {
379         case 0x00:
380                 string_desc = (const struct usb_string_descriptor *)
381                         rom.hw.usb.string0_desc;
382                 break;
383
384         case 0x10:
385                 string_desc = (const struct usb_string_descriptor *)
386                         rom.hw.usb.string1_desc;
387                 break;
388
389         case 0x20:
390                 string_desc = (const struct usb_string_descriptor *)
391                         rom.hw.usb.string2_desc;
392                 break;
393
394         case 0x30:
395                 string_desc = (const struct usb_string_descriptor *)
396                         rom.hw.usb.string3_desc;
397                 break;
398
399         default:
400                 break;
401         }
402
403         if (string_desc)
404                 return usb_ep0tx_data(string_desc, string_desc->bLength);
405
406         return -1;
407 }
408
409 static int usb_get_device_desc(const struct usb_ctrlrequest *ctrl __unused)
410 {
411         return usb_ep0tx_data(&rom.hw.usb.device_desc,
412                               rom.hw.usb.device_desc.bLength);
413 }
414
415 static int usb_get_config_desc(const struct usb_ctrlrequest *ctrl __unused)
416 {
417         fw.usb.cfg_desc->cfg.bDescriptorType = USB_DT_CONFIG;
418
419         return usb_ep0tx_data(fw.usb.cfg_desc,
420                 le16_to_cpu(fw.usb.cfg_desc->cfg.wTotalLength));
421 }
422
423 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
424 static int usb_get_otherspeed_desc(const struct usb_ctrlrequest *ctrl __unused)
425 {
426
427         fw.usb.os_cfg_desc->cfg.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
428
429         return usb_ep0tx_data(fw.usb.os_cfg_desc,
430                 le16_to_cpu(fw.usb.os_cfg_desc->cfg.wTotalLength));
431 }
432 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
433
434 static int usb_get_qualifier_desc(const struct usb_ctrlrequest *ctrl __unused)
435 {
436         struct usb_qualifier_descriptor qual;
437
438         /*
439          * The qualifier descriptor shares some structural details
440          * with the main device descriptor.
441          */
442
443         memcpy(&qual, &rom.hw.usb.device_desc, sizeof(qual));
444
445         /* (Re)-Initialize fields */
446         qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
447         qual.bLength = sizeof(qual);
448         qual.bNumConfigurations = rom.hw.usb.device_desc.bNumConfigurations;
449         qual.bRESERVED = 0;
450
451         return usb_ep0tx_data(&qual, qual.bLength);
452 }
453
454 #define USB_CHECK_REQTYPE(ctrl, recip, dir)                     \
455         (((ctrl->bRequestType & USB_RECIP_MASK) != recip) ||    \
456          ((ctrl->bRequestType & USB_DIR_MASK) != dir))
457
458 static int usb_get_descriptor(const struct usb_ctrlrequest *ctrl)
459 {
460         int status = -1;
461
462         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
463                 return status;
464
465         switch (le16_to_cpu(ctrl->wValue) >> 8) {
466         case USB_DT_DEVICE:
467                 status = usb_get_device_desc(ctrl);
468                 break;
469
470         case USB_DT_CONFIG:
471                 status = usb_get_config_desc(ctrl);
472                 break;
473
474         case USB_DT_STRING:
475                 status = usb_get_string_desc(ctrl);
476                 break;
477
478         case USB_DT_INTERFACE:
479                 break;
480
481         case USB_DT_ENDPOINT:
482                 break;
483
484         case USB_DT_DEVICE_QUALIFIER:
485                 status = usb_get_qualifier_desc(ctrl);
486                 break;
487
488 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
489         case USB_DT_OTHER_SPEED_CONFIG:
490                 status = usb_get_otherspeed_desc(ctrl);
491                 break;
492 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
493         default:
494                 break;
495
496         }
497
498         return status;
499 }
500
501 static int usb_get_configuration(const struct usb_ctrlrequest *ctrl)
502 {
503         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_IN))
504                 return -1;
505
506         return usb_ep0tx_data(&fw.usb.config, 1);
507 }
508
509 static int usb_set_configuration(const struct usb_ctrlrequest *ctrl)
510 {
511         unsigned int config;
512
513         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
514                 return -1;
515
516         config = le16_to_cpu(ctrl->wValue);
517         switch (config) {
518         case 0:
519                 /* Disable Device */
520                 andb(AR9170_USB_REG_DEVICE_ADDRESS,
521                       (uint8_t) ~(AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
522 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
523         case 1:
524                 fw.usb.config = config;
525
526                 if (usb_detect_highspeed()) {
527                         /* High Speed Configuration */
528                         usb_init_highspeed_fifo_cfg();
529                 } else {
530                         /* Full Speed Configuration */
531                         usb_init_fullspeed_fifo_cfg();
532                 }
533                 break;
534
535         default:
536                 return -1;
537         }
538         /* usb_pta_init() ? */
539
540         usb_reset_eps();
541         orb(AR9170_USB_REG_DEVICE_ADDRESS,
542             (AR9170_USB_DEVICE_ADDRESS_CONFIGURE));
543
544         usb_enable_global_int();
545         usb_trigger_out();
546         return 1;
547 #else
548         default:
549                 return -1;
550         }
551 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
552 }
553
554 static int usb_set_address(const struct usb_ctrlrequest *ctrl)
555 {
556         unsigned int address;
557
558         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_DEVICE, USB_DIR_OUT))
559                 return -1;
560
561         address = le16_to_cpu(ctrl->wValue);
562
563         /*
564          * The original firmware used 0x100 (which is, of course,
565          * too big to fit into uint8_t).
566          * However based on the available information (hw.h), BIT(7)
567          * is used as some sort of flag and should not be
568          * part of the device address.
569          */
570         if (address >= BIT(7))
571                 return -1;
572
573         setb(AR9170_USB_REG_DEVICE_ADDRESS, (uint8_t) address);
574         return 1;
575 }
576
577 static int usb_get_interface(const struct usb_ctrlrequest *ctrl)
578 {
579         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_IN))
580                 return -1;
581
582         if (usb_configured() == false)
583                 return -1;
584
585         switch (fw.usb.config) {
586         case 1:
587                 break;
588
589         default:
590                 return -1;
591         }
592
593         return usb_ep0tx_data(&fw.usb.alternate_interface_setting, 1);
594 }
595
596 static int usb_manipulate_feature(const struct usb_ctrlrequest *ctrl, bool __unused clear)
597 {
598         unsigned int feature;
599
600         if ((ctrl->bRequestType & USB_DIR_MASK) != USB_DIR_OUT)
601                 return -1;
602
603         if (usb_configured() == false)
604                 return -1;
605
606         feature = le16_to_cpu(ctrl->wValue);
607
608         switch (ctrl->bRequestType & USB_RECIP_MASK) {
609         case USB_RECIP_DEVICE: {
610 #ifdef CONFIG_CARL9170FW_WOL
611                 if (feature & USB_DEVICE_REMOTE_WAKEUP) {
612                         if (clear)
613                                 usb_disable_remote_wakeup();
614                         else
615                                 usb_enable_remote_wakeup();
616                 }
617 #endif /* CONFIG_CARL9170FW_WOL */
618
619                 if (clear)
620                         fw.usb.device_feature &= ~feature;
621                 else
622                         fw.usb.device_feature |= feature;
623
624
625                 break;
626                 }
627
628         case USB_RECIP_ENDPOINT: {
629                 unsigned int ep, dir;
630
631                 ep = le16_to_cpu(ctrl->wIndex) & 0xf;
632                 dir = le16_to_cpu(ctrl->wIndex) & USB_DIR_MASK;
633                 if (ep == 0) {
634                         /* According to the spec, EP cannot be stopped this way. */
635                         return -1;
636                 } else {
637                         unsigned int addr;
638
639                         if (dir == USB_DIR_IN)
640                                 addr = AR9170_USB_REG_EP_IN_MAX_SIZE_HIGH;
641                         else
642                                 addr = AR9170_USB_REG_EP_OUT_MAX_SIZE_HIGH;
643
644                         addr += (ep << 1);
645
646                         if (clear)
647                                 andb(addr, ~AR9170_USB_EP_OUT_STALL);
648                         else
649                                 orb(addr, AR9170_USB_EP_OUT_STALL);
650                 }
651                 break;
652                 }
653
654         case USB_RECIP_INTERFACE:
655                 /*
656                  * the current USB Specification Revision 2
657                  * specifies no interface features.
658                  */
659
660         default:
661                 return -1;
662         }
663
664         return 1;
665 }
666
667 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
668 static int usb_set_interface(const struct usb_ctrlrequest *ctrl)
669 {
670         unsigned int intf, alt_intf;
671         if (USB_CHECK_REQTYPE(ctrl, USB_RECIP_INTERFACE, USB_DIR_OUT))
672                 return -1;
673
674         if (usb_configured() == false)
675                 return -1;
676
677         intf = le16_to_cpu(ctrl->wIndex);
678         alt_intf = le16_to_cpu(ctrl->wValue);
679
680         switch (intf) {
681         case 0:
682                 if (alt_intf != fw.usb.cfg_desc->intf.bAlternateSetting)
683                         return -1;
684
685                 fw.usb.interface_setting = (uint8_t) intf;
686                 fw.usb.alternate_interface_setting = (uint8_t) alt_intf;
687                 if (usb_detect_highspeed())
688                         usb_init_highspeed_fifo_cfg();
689                 else
690                         usb_init_fullspeed_fifo_cfg();
691
692                 usb_reset_eps();
693                 usb_enable_global_int();
694                 usb_trigger_out();
695                 return 1;
696
697         default:
698                 return -1;
699         }
700 }
701 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
702 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
703
704 static int usb_standard_command(const struct usb_ctrlrequest *ctrl __unused)
705 {
706         int status = -1;
707
708 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
709         switch (ctrl->bRequest) {
710         case USB_REQ_GET_STATUS:
711                 status = usb_get_status(ctrl);
712                 break;
713
714         case USB_REQ_CLEAR_FEATURE:
715         case USB_REQ_SET_FEATURE:
716                 status = usb_manipulate_feature(ctrl, ctrl->bRequest == USB_REQ_CLEAR_FEATURE);
717                 break;
718
719         case USB_REQ_SET_ADDRESS:
720                 status = usb_set_address(ctrl);
721                 break;
722
723         case USB_REQ_GET_DESCRIPTOR:
724                 status = usb_get_descriptor(ctrl);
725                 break;
726
727         case USB_REQ_SET_DESCRIPTOR:
728                 break;
729
730         case USB_REQ_GET_CONFIGURATION:
731                 status = usb_get_configuration(ctrl);
732                 break;
733
734         case USB_REQ_SET_CONFIGURATION:
735                 status = usb_set_configuration(ctrl);
736                 break;
737
738         case USB_REQ_GET_INTERFACE:
739                 status = usb_get_interface(ctrl);
740                 break;
741
742         case USB_REQ_SET_INTERFACE:
743 #ifdef CONFIG_CARL9170FW_USB_MODESWITCH
744                 status = usb_set_interface(ctrl);
745 #endif /* CONFIG_CARL9170FW_USB_MODESWITCH */
746                 break;
747
748         case USB_REQ_SYNCH_FRAME:
749                 break;
750
751         default:
752                 break;
753
754         }
755 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
756
757         return status;
758 }
759
760 static int usb_class_command(const struct usb_ctrlrequest *ctrl __unused)
761 {
762         return -1;
763 }
764
765 static int usb_vendor_command(const struct usb_ctrlrequest *ctrl __unused)
766 {
767         /*
768          * Note: Firmware upload/boot is not implemented.
769          * It's impossible to replace the current image
770          * in place.
771          */
772
773         return -1;
774 }
775
776 #undef USB_CHECK_TYPE
777
778 void usb_ep0setup(void)
779 {
780         struct usb_ctrlrequest ctrl;
781         int status = -1;
782         usb_ep0rx_data(&ctrl, sizeof(ctrl));
783
784         switch (ctrl.bRequestType & USB_TYPE_MASK) {
785         case USB_TYPE_STANDARD:
786                 status = usb_standard_command(&ctrl);
787                 break;
788
789         case USB_TYPE_CLASS:
790                 status = usb_class_command(&ctrl);
791                 break;
792
793         case USB_TYPE_VENDOR:
794                 status = usb_vendor_command(&ctrl);
795                 break;
796
797         default:
798                 break;
799
800         }
801
802         if (status < 0)
803                 fw.usb.ep0_action |= CARL9170_EP0_STALL;
804 #ifdef CONFIG_CARL9170FW_USB_STANDARD_CMDS
805         if (status > 0)
806                 fw.usb.ep0_action |= CARL9170_EP0_TRIGGER;
807 #endif /* CONFIG_CARL9170FW_USB_STANDARD_CMDS */
808 }
809
810 void usb_ep0rx(void)
811 {
812         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
813                 return ;
814
815         usb_ep0rx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
816         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
817 }
818
819 void usb_ep0tx(void)
820 {
821         if (BUG_ON(!fw.usb.ep0_txrx_buffer || !fw.usb.ep0_txrx_len))
822                 return ;
823
824         usb_ep0tx_data(fw.usb.ep0_txrx_buffer, fw.usb.ep0_txrx_len);
825         fw.usb.ep0_txrx_pos = fw.usb.ep0_txrx_len;
826 }