]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/wireless/zd1211rw/zd_usb.c
Merge branch 'upstream-fixes' into upstream
[linux-2.6.git] / drivers / net / wireless / zd1211rw / zd_usb.c
1 /* zd_usb.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 #include <asm/unaligned.h>
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/firmware.h>
22 #include <linux/device.h>
23 #include <linux/errno.h>
24 #include <linux/skbuff.h>
25 #include <linux/usb.h>
26 #include <net/ieee80211.h>
27
28 #include "zd_def.h"
29 #include "zd_netdev.h"
30 #include "zd_mac.h"
31 #include "zd_usb.h"
32 #include "zd_util.h"
33
34 static struct usb_device_id usb_ids[] = {
35         /* ZD1211 */
36         { USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },
37         { USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },
38         { USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },
39         { USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },
40         { USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },
41         { USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },
42         { USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },
43         /* ZD1211B */
44         { USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },
45         { USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },
46         { USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },
47         {}
48 };
49
50 MODULE_LICENSE("GPL");
51 MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");
52 MODULE_AUTHOR("Ulrich Kunitz");
53 MODULE_AUTHOR("Daniel Drake");
54 MODULE_VERSION("1.0");
55 MODULE_DEVICE_TABLE(usb, usb_ids);
56
57 #define FW_ZD1211_PREFIX        "zd1211/zd1211_"
58 #define FW_ZD1211B_PREFIX       "zd1211/zd1211b_"
59
60 /* register address handling */
61
62 #ifdef DEBUG
63 static int check_addr(struct zd_usb *usb, zd_addr_t addr)
64 {
65         u32 base = ZD_ADDR_BASE(addr);
66         u32 offset = ZD_OFFSET(addr);
67
68         if ((u32)addr & ADDR_ZERO_MASK)
69                 goto invalid_address;
70         switch (base) {
71         case USB_BASE:
72                 break;
73         case CR_BASE:
74                 if (offset > CR_MAX_OFFSET) {
75                         dev_dbg(zd_usb_dev(usb),
76                                 "CR offset %#010x larger than"
77                                 " CR_MAX_OFFSET %#10x\n",
78                                 offset, CR_MAX_OFFSET);
79                         goto invalid_address;
80                 }
81                 if (offset & 1) {
82                         dev_dbg(zd_usb_dev(usb),
83                                 "CR offset %#010x is not a multiple of 2\n",
84                                 offset);
85                         goto invalid_address;
86                 }
87                 break;
88         case E2P_BASE:
89                 if (offset > E2P_MAX_OFFSET) {
90                         dev_dbg(zd_usb_dev(usb),
91                                 "E2P offset %#010x larger than"
92                                 " E2P_MAX_OFFSET %#010x\n",
93                                 offset, E2P_MAX_OFFSET);
94                         goto invalid_address;
95                 }
96                 break;
97         case FW_BASE:
98                 if (!usb->fw_base_offset) {
99                         dev_dbg(zd_usb_dev(usb),
100                                "ERROR: fw base offset has not been set\n");
101                         return -EAGAIN;
102                 }
103                 if (offset > FW_MAX_OFFSET) {
104                         dev_dbg(zd_usb_dev(usb),
105                                 "FW offset %#10x is larger than"
106                                 " FW_MAX_OFFSET %#010x\n",
107                                 offset, FW_MAX_OFFSET);
108                         goto invalid_address;
109                 }
110                 break;
111         default:
112                 dev_dbg(zd_usb_dev(usb),
113                         "address has unsupported base %#010x\n", addr);
114                 goto invalid_address;
115         }
116
117         return 0;
118 invalid_address:
119         dev_dbg(zd_usb_dev(usb),
120                 "ERROR: invalid address: %#010x\n", addr);
121         return -EINVAL;
122 }
123 #endif /* DEBUG */
124
125 static u16 usb_addr(struct zd_usb *usb, zd_addr_t addr)
126 {
127         u32 base;
128         u16 offset;
129
130         base = ZD_ADDR_BASE(addr);
131         offset = ZD_OFFSET(addr);
132
133         ZD_ASSERT(check_addr(usb, addr) == 0);
134
135         switch (base) {
136         case CR_BASE:
137                 offset += CR_BASE_OFFSET;
138                 break;
139         case E2P_BASE:
140                 offset += E2P_BASE_OFFSET;
141                 break;
142         case FW_BASE:
143                 offset += usb->fw_base_offset;
144                 break;
145         }
146
147         return offset;
148 }
149
150 /* USB device initialization */
151
152 static int request_fw_file(
153         const struct firmware **fw, const char *name, struct device *device)
154 {
155         int r;
156
157         dev_dbg_f(device, "fw name %s\n", name);
158
159         r = request_firmware(fw, name, device);
160         if (r)
161                 dev_err(device,
162                        "Could not load firmware file %s. Error number %d\n",
163                        name, r);
164         return r;
165 }
166
167 static inline u16 get_bcdDevice(const struct usb_device *udev)
168 {
169         return le16_to_cpu(udev->descriptor.bcdDevice);
170 }
171
172 enum upload_code_flags {
173         REBOOT = 1,
174 };
175
176 /* Ensures that MAX_TRANSFER_SIZE is even. */
177 #define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)
178
179 static int upload_code(struct usb_device *udev,
180         const u8 *data, size_t size, u16 code_offset, int flags)
181 {
182         u8 *p;
183         int r;
184
185         /* USB request blocks need "kmalloced" buffers.
186          */
187         p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);
188         if (!p) {
189                 dev_err(&udev->dev, "out of memory\n");
190                 r = -ENOMEM;
191                 goto error;
192         }
193
194         size &= ~1;
195         while (size > 0) {
196                 size_t transfer_size = size <= MAX_TRANSFER_SIZE ?
197                         size : MAX_TRANSFER_SIZE;
198
199                 dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);
200
201                 memcpy(p, data, transfer_size);
202                 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
203                         USB_REQ_FIRMWARE_DOWNLOAD,
204                         USB_DIR_OUT | USB_TYPE_VENDOR,
205                         code_offset, 0, p, transfer_size, 1000 /* ms */);
206                 if (r < 0) {
207                         dev_err(&udev->dev,
208                                "USB control request for firmware upload"
209                                " failed. Error number %d\n", r);
210                         goto error;
211                 }
212                 transfer_size = r & ~1;
213
214                 size -= transfer_size;
215                 data += transfer_size;
216                 code_offset += transfer_size/sizeof(u16);
217         }
218
219         if (flags & REBOOT) {
220                 u8 ret;
221
222                 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
223                         USB_REQ_FIRMWARE_CONFIRM,
224                         USB_DIR_IN | USB_TYPE_VENDOR,
225                         0, 0, &ret, sizeof(ret), 5000 /* ms */);
226                 if (r != sizeof(ret)) {
227                         dev_err(&udev->dev,
228                                 "control request firmeware confirmation failed."
229                                 " Return value %d\n", r);
230                         if (r >= 0)
231                                 r = -ENODEV;
232                         goto error;
233                 }
234                 if (ret & 0x80) {
235                         dev_err(&udev->dev,
236                                 "Internal error while downloading."
237                                 " Firmware confirm return value %#04x\n",
238                                 (unsigned int)ret);
239                         r = -ENODEV;
240                         goto error;
241                 }
242                 dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",
243                         (unsigned int)ret);
244         }
245
246         r = 0;
247 error:
248         kfree(p);
249         return r;
250 }
251
252 static u16 get_word(const void *data, u16 offset)
253 {
254         const __le16 *p = data;
255         return le16_to_cpu(p[offset]);
256 }
257
258 static char *get_fw_name(char *buffer, size_t size, u8 device_type,
259                        const char* postfix)
260 {
261         scnprintf(buffer, size, "%s%s",
262                 device_type == DEVICE_ZD1211B ?
263                         FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,
264                 postfix);
265         return buffer;
266 }
267
268 static int upload_firmware(struct usb_device *udev, u8 device_type)
269 {
270         int r;
271         u16 fw_bcdDevice;
272         u16 bcdDevice;
273         const struct firmware *ub_fw = NULL;
274         const struct firmware *uph_fw = NULL;
275         char fw_name[128];
276
277         bcdDevice = get_bcdDevice(udev);
278
279         r = request_fw_file(&ub_fw,
280                 get_fw_name(fw_name, sizeof(fw_name), device_type,  "ub"),
281                 &udev->dev);
282         if (r)
283                 goto error;
284
285         fw_bcdDevice = get_word(ub_fw->data, EEPROM_REGS_OFFSET);
286
287         /* FIXME: do we have any reason to perform the kludge that the vendor
288          * driver does when there is a version mismatch? (their driver uploads
289          * different firmwares and stuff)
290          */
291         if (fw_bcdDevice != bcdDevice) {
292                 dev_info(&udev->dev,
293                         "firmware device id %#06x and actual device id "
294                         "%#06x differ, continuing anyway\n",
295                         fw_bcdDevice, bcdDevice);
296         } else {
297                 dev_dbg_f(&udev->dev,
298                         "firmware device id %#06x is equal to the "
299                         "actual device id\n", fw_bcdDevice);
300         }
301
302
303         r = request_fw_file(&uph_fw,
304                 get_fw_name(fw_name, sizeof(fw_name), device_type, "uphr"),
305                 &udev->dev);
306         if (r)
307                 goto error;
308
309         r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START_OFFSET,
310                         REBOOT);
311         if (r) {
312                 dev_err(&udev->dev,
313                         "Could not upload firmware code uph. Error number %d\n",
314                         r);
315         }
316
317         /* FALL-THROUGH */
318 error:
319         release_firmware(ub_fw);
320         release_firmware(uph_fw);
321         return r;
322 }
323
324 static void disable_read_regs_int(struct zd_usb *usb)
325 {
326         struct zd_usb_interrupt *intr = &usb->intr;
327
328         spin_lock(&intr->lock);
329         intr->read_regs_enabled = 0;
330         spin_unlock(&intr->lock);
331 }
332
333 #define urb_dev(urb) (&(urb)->dev->dev)
334
335 static inline void handle_regs_int(struct urb *urb)
336 {
337         struct zd_usb *usb = urb->context;
338         struct zd_usb_interrupt *intr = &usb->intr;
339         int len;
340
341         ZD_ASSERT(in_interrupt());
342         spin_lock(&intr->lock);
343
344         if (intr->read_regs_enabled) {
345                 intr->read_regs.length = len = urb->actual_length;
346
347                 if (len > sizeof(intr->read_regs.buffer))
348                         len = sizeof(intr->read_regs.buffer);
349                 memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);
350                 intr->read_regs_enabled = 0;
351                 complete(&intr->read_regs.completion);
352                 goto out;
353         }
354
355         dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");
356 out:
357         spin_unlock(&intr->lock);
358 }
359
360 static inline void handle_retry_failed_int(struct urb *urb)
361 {
362         dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");
363 }
364
365
366 static void int_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
367 {
368         int r;
369         struct usb_int_header *hdr;
370
371         switch (urb->status) {
372         case 0:
373                 break;
374         case -ESHUTDOWN:
375         case -EINVAL:
376         case -ENODEV:
377         case -ENOENT:
378         case -ECONNRESET:
379         case -EPIPE:
380                 goto kfree;
381         default:
382                 goto resubmit;
383         }
384
385         if (urb->actual_length < sizeof(hdr)) {
386                 dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);
387                 goto resubmit;
388         }
389
390         hdr = urb->transfer_buffer;
391         if (hdr->type != USB_INT_TYPE) {
392                 dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);
393                 goto resubmit;
394         }
395
396         switch (hdr->id) {
397         case USB_INT_ID_REGS:
398                 handle_regs_int(urb);
399                 break;
400         case USB_INT_ID_RETRY_FAILED:
401                 handle_retry_failed_int(urb);
402                 break;
403         default:
404                 dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,
405                         (unsigned int)hdr->id);
406                 goto resubmit;
407         }
408
409 resubmit:
410         r = usb_submit_urb(urb, GFP_ATOMIC);
411         if (r) {
412                 dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);
413                 goto kfree;
414         }
415         return;
416 kfree:
417         kfree(urb->transfer_buffer);
418 }
419
420 static inline int int_urb_interval(struct usb_device *udev)
421 {
422         switch (udev->speed) {
423         case USB_SPEED_HIGH:
424                 return 4;
425         case USB_SPEED_LOW:
426                 return 10;
427         case USB_SPEED_FULL:
428         default:
429                 return 1;
430         }
431 }
432
433 static inline int usb_int_enabled(struct zd_usb *usb)
434 {
435         unsigned long flags;
436         struct zd_usb_interrupt *intr = &usb->intr;
437         struct urb *urb;
438
439         spin_lock_irqsave(&intr->lock, flags);
440         urb = intr->urb;
441         spin_unlock_irqrestore(&intr->lock, flags);
442         return urb != NULL;
443 }
444
445 int zd_usb_enable_int(struct zd_usb *usb)
446 {
447         int r;
448         struct usb_device *udev;
449         struct zd_usb_interrupt *intr = &usb->intr;
450         void *transfer_buffer = NULL;
451         struct urb *urb;
452
453         dev_dbg_f(zd_usb_dev(usb), "\n");
454
455         urb = usb_alloc_urb(0, GFP_NOFS);
456         if (!urb) {
457                 r = -ENOMEM;
458                 goto out;
459         }
460
461         ZD_ASSERT(!irqs_disabled());
462         spin_lock_irq(&intr->lock);
463         if (intr->urb) {
464                 spin_unlock_irq(&intr->lock);
465                 r = 0;
466                 goto error_free_urb;
467         }
468         intr->urb = urb;
469         spin_unlock_irq(&intr->lock);
470
471         /* TODO: make it a DMA buffer */
472         r = -ENOMEM;
473         transfer_buffer = kmalloc(USB_MAX_EP_INT_BUFFER, GFP_NOFS);
474         if (!transfer_buffer) {
475                 dev_dbg_f(zd_usb_dev(usb),
476                         "couldn't allocate transfer_buffer\n");
477                 goto error_set_urb_null;
478         }
479
480         udev = zd_usb_to_usbdev(usb);
481         usb_fill_int_urb(urb, udev, usb_rcvintpipe(udev, EP_INT_IN),
482                          transfer_buffer, USB_MAX_EP_INT_BUFFER,
483                          int_urb_complete, usb,
484                          intr->interval);
485
486         dev_dbg_f(zd_usb_dev(usb), "submit urb %p\n", intr->urb);
487         r = usb_submit_urb(urb, GFP_NOFS);
488         if (r) {
489                 dev_dbg_f(zd_usb_dev(usb),
490                          "Couldn't submit urb. Error number %d\n", r);
491                 goto error;
492         }
493
494         return 0;
495 error:
496         kfree(transfer_buffer);
497 error_set_urb_null:
498         spin_lock_irq(&intr->lock);
499         intr->urb = NULL;
500         spin_unlock_irq(&intr->lock);
501 error_free_urb:
502         usb_free_urb(urb);
503 out:
504         return r;
505 }
506
507 void zd_usb_disable_int(struct zd_usb *usb)
508 {
509         unsigned long flags;
510         struct zd_usb_interrupt *intr = &usb->intr;
511         struct urb *urb;
512
513         spin_lock_irqsave(&intr->lock, flags);
514         urb = intr->urb;
515         if (!urb) {
516                 spin_unlock_irqrestore(&intr->lock, flags);
517                 return;
518         }
519         intr->urb = NULL;
520         spin_unlock_irqrestore(&intr->lock, flags);
521
522         usb_kill_urb(urb);
523         dev_dbg_f(zd_usb_dev(usb), "urb %p killed\n", urb);
524         usb_free_urb(urb);
525 }
526
527 static void handle_rx_packet(struct zd_usb *usb, const u8 *buffer,
528                              unsigned int length)
529 {
530         int i;
531         struct zd_mac *mac = zd_usb_to_mac(usb);
532         const struct rx_length_info *length_info;
533
534         if (length < sizeof(struct rx_length_info)) {
535                 /* It's not a complete packet anyhow. */
536                 return;
537         }
538         length_info = (struct rx_length_info *)
539                 (buffer + length - sizeof(struct rx_length_info));
540
541         /* It might be that three frames are merged into a single URB
542          * transaction. We have to check for the length info tag.
543          *
544          * While testing we discovered that length_info might be unaligned,
545          * because if USB transactions are merged, the last packet will not
546          * be padded. Unaligned access might also happen if the length_info
547          * structure is not present.
548          */
549         if (get_unaligned(&length_info->tag) == cpu_to_le16(RX_LENGTH_INFO_TAG))
550         {
551                 unsigned int l, k, n;
552                 for (i = 0, l = 0;; i++) {
553                         k = le16_to_cpu(get_unaligned(&length_info->length[i]));
554                         n = l+k;
555                         if (n > length)
556                                 return;
557                         zd_mac_rx(mac, buffer+l, k);
558                         if (i >= 2)
559                                 return;
560                         l = (n+3) & ~3;
561                 }
562         } else {
563                 zd_mac_rx(mac, buffer, length);
564         }
565 }
566
567 static void rx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
568 {
569         struct zd_usb *usb;
570         struct zd_usb_rx *rx;
571         const u8 *buffer;
572         unsigned int length;
573
574         switch (urb->status) {
575         case 0:
576                 break;
577         case -ESHUTDOWN:
578         case -EINVAL:
579         case -ENODEV:
580         case -ENOENT:
581         case -ECONNRESET:
582         case -EPIPE:
583                 return;
584         default:
585                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
586                 goto resubmit;
587         }
588
589         buffer = urb->transfer_buffer;
590         length = urb->actual_length;
591         usb = urb->context;
592         rx = &usb->rx;
593
594         if (length%rx->usb_packet_size > rx->usb_packet_size-4) {
595                 /* If there is an old first fragment, we don't care. */
596                 dev_dbg_f(urb_dev(urb), "*** first fragment ***\n");
597                 ZD_ASSERT(length <= ARRAY_SIZE(rx->fragment));
598                 spin_lock(&rx->lock);
599                 memcpy(rx->fragment, buffer, length);
600                 rx->fragment_length = length;
601                 spin_unlock(&rx->lock);
602                 goto resubmit;
603         }
604
605         spin_lock(&rx->lock);
606         if (rx->fragment_length > 0) {
607                 /* We are on a second fragment, we believe */
608                 ZD_ASSERT(length + rx->fragment_length <=
609                           ARRAY_SIZE(rx->fragment));
610                 dev_dbg_f(urb_dev(urb), "*** second fragment ***\n");
611                 memcpy(rx->fragment+rx->fragment_length, buffer, length);
612                 handle_rx_packet(usb, rx->fragment,
613                                  rx->fragment_length + length);
614                 rx->fragment_length = 0;
615                 spin_unlock(&rx->lock);
616         } else {
617                 spin_unlock(&rx->lock);
618                 handle_rx_packet(usb, buffer, length);
619         }
620
621 resubmit:
622         usb_submit_urb(urb, GFP_ATOMIC);
623 }
624
625 struct urb *alloc_urb(struct zd_usb *usb)
626 {
627         struct usb_device *udev = zd_usb_to_usbdev(usb);
628         struct urb *urb;
629         void *buffer;
630
631         urb = usb_alloc_urb(0, GFP_NOFS);
632         if (!urb)
633                 return NULL;
634         buffer = usb_buffer_alloc(udev, USB_MAX_RX_SIZE, GFP_NOFS,
635                                   &urb->transfer_dma);
636         if (!buffer) {
637                 usb_free_urb(urb);
638                 return NULL;
639         }
640
641         usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, EP_DATA_IN),
642                           buffer, USB_MAX_RX_SIZE,
643                           rx_urb_complete, usb);
644         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
645
646         return urb;
647 }
648
649 void free_urb(struct urb *urb)
650 {
651         if (!urb)
652                 return;
653         usb_buffer_free(urb->dev, urb->transfer_buffer_length,
654                         urb->transfer_buffer, urb->transfer_dma);
655         usb_free_urb(urb);
656 }
657
658 int zd_usb_enable_rx(struct zd_usb *usb)
659 {
660         int i, r;
661         struct zd_usb_rx *rx = &usb->rx;
662         struct urb **urbs;
663
664         dev_dbg_f(zd_usb_dev(usb), "\n");
665
666         r = -ENOMEM;
667         urbs = kcalloc(URBS_COUNT, sizeof(struct urb *), GFP_NOFS);
668         if (!urbs)
669                 goto error;
670         for (i = 0; i < URBS_COUNT; i++) {
671                 urbs[i] = alloc_urb(usb);
672                 if (!urbs[i])
673                         goto error;
674         }
675
676         ZD_ASSERT(!irqs_disabled());
677         spin_lock_irq(&rx->lock);
678         if (rx->urbs) {
679                 spin_unlock_irq(&rx->lock);
680                 r = 0;
681                 goto error;
682         }
683         rx->urbs = urbs;
684         rx->urbs_count = URBS_COUNT;
685         spin_unlock_irq(&rx->lock);
686
687         for (i = 0; i < URBS_COUNT; i++) {
688                 r = usb_submit_urb(urbs[i], GFP_NOFS);
689                 if (r)
690                         goto error_submit;
691         }
692
693         return 0;
694 error_submit:
695         for (i = 0; i < URBS_COUNT; i++) {
696                 usb_kill_urb(urbs[i]);
697         }
698         spin_lock_irq(&rx->lock);
699         rx->urbs = NULL;
700         rx->urbs_count = 0;
701         spin_unlock_irq(&rx->lock);
702 error:
703         if (urbs) {
704                 for (i = 0; i < URBS_COUNT; i++)
705                         free_urb(urbs[i]);
706         }
707         return r;
708 }
709
710 void zd_usb_disable_rx(struct zd_usb *usb)
711 {
712         int i;
713         unsigned long flags;
714         struct urb **urbs;
715         unsigned int count;
716         struct zd_usb_rx *rx = &usb->rx;
717
718         spin_lock_irqsave(&rx->lock, flags);
719         urbs = rx->urbs;
720         count = rx->urbs_count;
721         spin_unlock_irqrestore(&rx->lock, flags);
722         if (!urbs)
723                 return;
724
725         for (i = 0; i < count; i++) {
726                 usb_kill_urb(urbs[i]);
727                 free_urb(urbs[i]);
728         }
729         kfree(urbs);
730
731         spin_lock_irqsave(&rx->lock, flags);
732         rx->urbs = NULL;
733         rx->urbs_count = 0;
734         spin_unlock_irqrestore(&rx->lock, flags);
735 }
736
737 static void tx_urb_complete(struct urb *urb, struct pt_regs *pt_regs)
738 {
739         int r;
740
741         switch (urb->status) {
742         case 0:
743                 break;
744         case -ESHUTDOWN:
745         case -EINVAL:
746         case -ENODEV:
747         case -ENOENT:
748         case -ECONNRESET:
749         case -EPIPE:
750                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
751                 break;
752         default:
753                 dev_dbg_f(urb_dev(urb), "urb %p error %d\n", urb, urb->status);
754                 goto resubmit;
755         }
756 free_urb:
757         usb_buffer_free(urb->dev, urb->transfer_buffer_length,
758                         urb->transfer_buffer, urb->transfer_dma);
759         usb_free_urb(urb);
760         return;
761 resubmit:
762         r = usb_submit_urb(urb, GFP_ATOMIC);
763         if (r) {
764                 dev_dbg_f(urb_dev(urb), "error resubmit urb %p %d\n", urb, r);
765                 goto free_urb;
766         }
767 }
768
769 /* Puts the frame on the USB endpoint. It doesn't wait for
770  * completion. The frame must contain the control set.
771  */
772 int zd_usb_tx(struct zd_usb *usb, const u8 *frame, unsigned int length)
773 {
774         int r;
775         struct usb_device *udev = zd_usb_to_usbdev(usb);
776         struct urb *urb;
777         void *buffer;
778
779         urb = usb_alloc_urb(0, GFP_ATOMIC);
780         if (!urb) {
781                 r = -ENOMEM;
782                 goto out;
783         }
784
785         buffer = usb_buffer_alloc(zd_usb_to_usbdev(usb), length, GFP_ATOMIC,
786                                   &urb->transfer_dma);
787         if (!buffer) {
788                 r = -ENOMEM;
789                 goto error_free_urb;
790         }
791         memcpy(buffer, frame, length);
792
793         usb_fill_bulk_urb(urb, udev, usb_sndbulkpipe(udev, EP_DATA_OUT),
794                           buffer, length, tx_urb_complete, NULL);
795         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
796
797         r = usb_submit_urb(urb, GFP_ATOMIC);
798         if (r)
799                 goto error;
800         return 0;
801 error:
802         usb_buffer_free(zd_usb_to_usbdev(usb), length, buffer,
803                         urb->transfer_dma);
804 error_free_urb:
805         usb_free_urb(urb);
806 out:
807         return r;
808 }
809
810 static inline void init_usb_interrupt(struct zd_usb *usb)
811 {
812         struct zd_usb_interrupt *intr = &usb->intr;
813
814         spin_lock_init(&intr->lock);
815         intr->interval = int_urb_interval(zd_usb_to_usbdev(usb));
816         init_completion(&intr->read_regs.completion);
817         intr->read_regs.cr_int_addr = cpu_to_le16(usb_addr(usb, CR_INTERRUPT));
818 }
819
820 static inline void init_usb_rx(struct zd_usb *usb)
821 {
822         struct zd_usb_rx *rx = &usb->rx;
823         spin_lock_init(&rx->lock);
824         if (interface_to_usbdev(usb->intf)->speed == USB_SPEED_HIGH) {
825                 rx->usb_packet_size = 512;
826         } else {
827                 rx->usb_packet_size = 64;
828         }
829         ZD_ASSERT(rx->fragment_length == 0);
830 }
831
832 static inline void init_usb_tx(struct zd_usb *usb)
833 {
834         /* FIXME: at this point we will allocate a fixed number of urb's for
835          * use in a cyclic scheme */
836 }
837
838 void zd_usb_init(struct zd_usb *usb, struct net_device *netdev,
839                  struct usb_interface *intf)
840 {
841         memset(usb, 0, sizeof(*usb));
842         usb->intf = usb_get_intf(intf);
843         usb_set_intfdata(usb->intf, netdev);
844         init_usb_interrupt(usb);
845         init_usb_tx(usb);
846         init_usb_rx(usb);
847 }
848
849 int zd_usb_init_hw(struct zd_usb *usb)
850 {
851         int r;
852         struct zd_chip *chip = zd_usb_to_chip(usb);
853
854         ZD_ASSERT(mutex_is_locked(&chip->mutex));
855         r = zd_ioread16_locked(chip, &usb->fw_base_offset,
856                         USB_REG((u16)FW_BASE_ADDR_OFFSET));
857         if (r)
858                 return r;
859         dev_dbg_f(zd_usb_dev(usb), "fw_base_offset: %#06hx\n",
860                  usb->fw_base_offset);
861
862         return 0;
863 }
864
865 void zd_usb_clear(struct zd_usb *usb)
866 {
867         usb_set_intfdata(usb->intf, NULL);
868         usb_put_intf(usb->intf);
869         memset(usb, 0, sizeof(*usb));
870         /* FIXME: usb_interrupt, usb_tx, usb_rx? */
871 }
872
873 static const char *speed(enum usb_device_speed speed)
874 {
875         switch (speed) {
876         case USB_SPEED_LOW:
877                 return "low";
878         case USB_SPEED_FULL:
879                 return "full";
880         case USB_SPEED_HIGH:
881                 return "high";
882         default:
883                 return "unknown speed";
884         }
885 }
886
887 static int scnprint_id(struct usb_device *udev, char *buffer, size_t size)
888 {
889         return scnprintf(buffer, size, "%04hx:%04hx v%04hx %s",
890                 le16_to_cpu(udev->descriptor.idVendor),
891                 le16_to_cpu(udev->descriptor.idProduct),
892                 get_bcdDevice(udev),
893                 speed(udev->speed));
894 }
895
896 int zd_usb_scnprint_id(struct zd_usb *usb, char *buffer, size_t size)
897 {
898         struct usb_device *udev = interface_to_usbdev(usb->intf);
899         return scnprint_id(udev, buffer, size);
900 }
901
902 #ifdef DEBUG
903 static void print_id(struct usb_device *udev)
904 {
905         char buffer[40];
906
907         scnprint_id(udev, buffer, sizeof(buffer));
908         buffer[sizeof(buffer)-1] = 0;
909         dev_dbg_f(&udev->dev, "%s\n", buffer);
910 }
911 #else
912 #define print_id(udev) do { } while (0)
913 #endif
914
915 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
916 {
917         int r;
918         struct usb_device *udev = interface_to_usbdev(intf);
919         struct net_device *netdev = NULL;
920
921         print_id(udev);
922
923         switch (udev->speed) {
924         case USB_SPEED_LOW:
925         case USB_SPEED_FULL:
926         case USB_SPEED_HIGH:
927                 break;
928         default:
929                 dev_dbg_f(&intf->dev, "Unknown USB speed\n");
930                 r = -ENODEV;
931                 goto error;
932         }
933
934         netdev = zd_netdev_alloc(intf);
935         if (netdev == NULL) {
936                 r = -ENOMEM;
937                 goto error;
938         }
939
940         r = upload_firmware(udev, id->driver_info);
941         if (r) {
942                 dev_err(&intf->dev,
943                        "couldn't load firmware. Error number %d\n", r);
944                 goto error;
945         }
946
947         r = usb_reset_configuration(udev);
948         if (r) {
949                 dev_dbg_f(&intf->dev,
950                         "couldn't reset configuration. Error number %d\n", r);
951                 goto error;
952         }
953
954         /* At this point the interrupt endpoint is not generally enabled. We
955          * save the USB bandwidth until the network device is opened. But
956          * notify that the initialization of the MAC will require the
957          * interrupts to be temporary enabled.
958          */
959         r = zd_mac_init_hw(zd_netdev_mac(netdev), id->driver_info);
960         if (r) {
961                 dev_dbg_f(&intf->dev,
962                          "couldn't initialize mac. Error number %d\n", r);
963                 goto error;
964         }
965
966         r = register_netdev(netdev);
967         if (r) {
968                 dev_dbg_f(&intf->dev,
969                          "couldn't register netdev. Error number %d\n", r);
970                 goto error;
971         }
972
973         dev_dbg_f(&intf->dev, "successful\n");
974         dev_info(&intf->dev,"%s\n", netdev->name);
975         return 0;
976 error:
977         usb_reset_device(interface_to_usbdev(intf));
978         zd_netdev_free(netdev);
979         return r;
980 }
981
982 static void disconnect(struct usb_interface *intf)
983 {
984         struct net_device *netdev = zd_intf_to_netdev(intf);
985         struct zd_mac *mac = zd_netdev_mac(netdev);
986         struct zd_usb *usb = &mac->chip.usb;
987
988         dev_dbg_f(zd_usb_dev(usb), "\n");
989
990         zd_netdev_disconnect(netdev);
991
992         /* Just in case something has gone wrong! */
993         zd_usb_disable_rx(usb);
994         zd_usb_disable_int(usb);
995
996         /* If the disconnect has been caused by a removal of the
997          * driver module, the reset allows reloading of the driver. If the
998          * reset will not be executed here, the upload of the firmware in the
999          * probe function caused by the reloading of the driver will fail.
1000          */
1001         usb_reset_device(interface_to_usbdev(intf));
1002
1003         /* If somebody still waits on this lock now, this is an error. */
1004         zd_netdev_free(netdev);
1005         dev_dbg(&intf->dev, "disconnected\n");
1006 }
1007
1008 static struct usb_driver driver = {
1009         .name           = "zd1211rw",
1010         .id_table       = usb_ids,
1011         .probe          = probe,
1012         .disconnect     = disconnect,
1013 };
1014
1015 static int __init usb_init(void)
1016 {
1017         int r;
1018
1019         pr_debug("usb_init()\n");
1020
1021         r = usb_register(&driver);
1022         if (r) {
1023                 printk(KERN_ERR "usb_register() failed. Error number %d\n", r);
1024                 return r;
1025         }
1026
1027         pr_debug("zd1211rw initialized\n");
1028         return 0;
1029 }
1030
1031 static void __exit usb_exit(void)
1032 {
1033         pr_debug("usb_exit()\n");
1034         usb_deregister(&driver);
1035 }
1036
1037 module_init(usb_init);
1038 module_exit(usb_exit);
1039
1040 static int usb_int_regs_length(unsigned int count)
1041 {
1042         return sizeof(struct usb_int_regs) + count * sizeof(struct reg_data);
1043 }
1044
1045 static void prepare_read_regs_int(struct zd_usb *usb)
1046 {
1047         struct zd_usb_interrupt *intr = &usb->intr;
1048
1049         spin_lock(&intr->lock);
1050         intr->read_regs_enabled = 1;
1051         INIT_COMPLETION(intr->read_regs.completion);
1052         spin_unlock(&intr->lock);
1053 }
1054
1055 static int get_results(struct zd_usb *usb, u16 *values,
1056                        struct usb_req_read_regs *req, unsigned int count)
1057 {
1058         int r;
1059         int i;
1060         struct zd_usb_interrupt *intr = &usb->intr;
1061         struct read_regs_int *rr = &intr->read_regs;
1062         struct usb_int_regs *regs = (struct usb_int_regs *)rr->buffer;
1063
1064         spin_lock(&intr->lock);
1065
1066         r = -EIO;
1067         /* The created block size seems to be larger than expected.
1068          * However results appear to be correct.
1069          */
1070         if (rr->length < usb_int_regs_length(count)) {
1071                 dev_dbg_f(zd_usb_dev(usb),
1072                          "error: actual length %d less than expected %d\n",
1073                          rr->length, usb_int_regs_length(count));
1074                 goto error_unlock;
1075         }
1076         if (rr->length > sizeof(rr->buffer)) {
1077                 dev_dbg_f(zd_usb_dev(usb),
1078                          "error: actual length %d exceeds buffer size %zu\n",
1079                          rr->length, sizeof(rr->buffer));
1080                 goto error_unlock;
1081         }
1082
1083         for (i = 0; i < count; i++) {
1084                 struct reg_data *rd = &regs->regs[i];
1085                 if (rd->addr != req->addr[i]) {
1086                         dev_dbg_f(zd_usb_dev(usb),
1087                                  "rd[%d] addr %#06hx expected %#06hx\n", i,
1088                                  le16_to_cpu(rd->addr),
1089                                  le16_to_cpu(req->addr[i]));
1090                         goto error_unlock;
1091                 }
1092                 values[i] = le16_to_cpu(rd->value);
1093         }
1094
1095         r = 0;
1096 error_unlock:
1097         spin_unlock(&intr->lock);
1098         return r;
1099 }
1100
1101 int zd_usb_ioread16v(struct zd_usb *usb, u16 *values,
1102                      const zd_addr_t *addresses, unsigned int count)
1103 {
1104         int r;
1105         int i, req_len, actual_req_len;
1106         struct usb_device *udev;
1107         struct usb_req_read_regs *req = NULL;
1108         unsigned long timeout;
1109
1110         if (count < 1) {
1111                 dev_dbg_f(zd_usb_dev(usb), "error: count is zero\n");
1112                 return -EINVAL;
1113         }
1114         if (count > USB_MAX_IOREAD16_COUNT) {
1115                 dev_dbg_f(zd_usb_dev(usb),
1116                          "error: count %u exceeds possible max %u\n",
1117                          count, USB_MAX_IOREAD16_COUNT);
1118                 return -EINVAL;
1119         }
1120         if (in_atomic()) {
1121                 dev_dbg_f(zd_usb_dev(usb),
1122                          "error: io in atomic context not supported\n");
1123                 return -EWOULDBLOCK;
1124         }
1125         if (!usb_int_enabled(usb)) {
1126                  dev_dbg_f(zd_usb_dev(usb),
1127                           "error: usb interrupt not enabled\n");
1128                 return -EWOULDBLOCK;
1129         }
1130
1131         req_len = sizeof(struct usb_req_read_regs) + count * sizeof(__le16);
1132         req = kmalloc(req_len, GFP_NOFS);
1133         if (!req)
1134                 return -ENOMEM;
1135         req->id = cpu_to_le16(USB_REQ_READ_REGS);
1136         for (i = 0; i < count; i++)
1137                 req->addr[i] = cpu_to_le16(usb_addr(usb, addresses[i]));
1138
1139         udev = zd_usb_to_usbdev(usb);
1140         prepare_read_regs_int(usb);
1141         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1142                          req, req_len, &actual_req_len, 1000 /* ms */);
1143         if (r) {
1144                 dev_dbg_f(zd_usb_dev(usb),
1145                         "error in usb_bulk_msg(). Error number %d\n", r);
1146                 goto error;
1147         }
1148         if (req_len != actual_req_len) {
1149                 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()\n"
1150                         " req_len %d != actual_req_len %d\n",
1151                         req_len, actual_req_len);
1152                 r = -EIO;
1153                 goto error;
1154         }
1155
1156         timeout = wait_for_completion_timeout(&usb->intr.read_regs.completion,
1157                                               msecs_to_jiffies(1000));
1158         if (!timeout) {
1159                 disable_read_regs_int(usb);
1160                 dev_dbg_f(zd_usb_dev(usb), "read timed out\n");
1161                 r = -ETIMEDOUT;
1162                 goto error;
1163         }
1164
1165         r = get_results(usb, values, req, count);
1166 error:
1167         kfree(req);
1168         return r;
1169 }
1170
1171 int zd_usb_iowrite16v(struct zd_usb *usb, const struct zd_ioreq16 *ioreqs,
1172                       unsigned int count)
1173 {
1174         int r;
1175         struct usb_device *udev;
1176         struct usb_req_write_regs *req = NULL;
1177         int i, req_len, actual_req_len;
1178
1179         if (count == 0)
1180                 return 0;
1181         if (count > USB_MAX_IOWRITE16_COUNT) {
1182                 dev_dbg_f(zd_usb_dev(usb),
1183                         "error: count %u exceeds possible max %u\n",
1184                         count, USB_MAX_IOWRITE16_COUNT);
1185                 return -EINVAL;
1186         }
1187         if (in_atomic()) {
1188                 dev_dbg_f(zd_usb_dev(usb),
1189                         "error: io in atomic context not supported\n");
1190                 return -EWOULDBLOCK;
1191         }
1192
1193         req_len = sizeof(struct usb_req_write_regs) +
1194                   count * sizeof(struct reg_data);
1195         req = kmalloc(req_len, GFP_NOFS);
1196         if (!req)
1197                 return -ENOMEM;
1198
1199         req->id = cpu_to_le16(USB_REQ_WRITE_REGS);
1200         for (i = 0; i < count; i++) {
1201                 struct reg_data *rw  = &req->reg_writes[i];
1202                 rw->addr = cpu_to_le16(usb_addr(usb, ioreqs[i].addr));
1203                 rw->value = cpu_to_le16(ioreqs[i].value);
1204         }
1205
1206         udev = zd_usb_to_usbdev(usb);
1207         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1208                          req, req_len, &actual_req_len, 1000 /* ms */);
1209         if (r) {
1210                 dev_dbg_f(zd_usb_dev(usb),
1211                         "error in usb_bulk_msg(). Error number %d\n", r);
1212                 goto error;
1213         }
1214         if (req_len != actual_req_len) {
1215                 dev_dbg_f(zd_usb_dev(usb),
1216                         "error in usb_bulk_msg()"
1217                         " req_len %d != actual_req_len %d\n",
1218                         req_len, actual_req_len);
1219                 r = -EIO;
1220                 goto error;
1221         }
1222
1223         /* FALL-THROUGH with r == 0 */
1224 error:
1225         kfree(req);
1226         return r;
1227 }
1228
1229 int zd_usb_rfwrite(struct zd_usb *usb, u32 value, u8 bits)
1230 {
1231         int r;
1232         struct usb_device *udev;
1233         struct usb_req_rfwrite *req = NULL;
1234         int i, req_len, actual_req_len;
1235         u16 bit_value_template;
1236
1237         if (in_atomic()) {
1238                 dev_dbg_f(zd_usb_dev(usb),
1239                         "error: io in atomic context not supported\n");
1240                 return -EWOULDBLOCK;
1241         }
1242         if (bits < USB_MIN_RFWRITE_BIT_COUNT) {
1243                 dev_dbg_f(zd_usb_dev(usb),
1244                         "error: bits %d are smaller than"
1245                         " USB_MIN_RFWRITE_BIT_COUNT %d\n",
1246                         bits, USB_MIN_RFWRITE_BIT_COUNT);
1247                 return -EINVAL;
1248         }
1249         if (bits > USB_MAX_RFWRITE_BIT_COUNT) {
1250                 dev_dbg_f(zd_usb_dev(usb),
1251                         "error: bits %d exceed USB_MAX_RFWRITE_BIT_COUNT %d\n",
1252                         bits, USB_MAX_RFWRITE_BIT_COUNT);
1253                 return -EINVAL;
1254         }
1255 #ifdef DEBUG
1256         if (value & (~0UL << bits)) {
1257                 dev_dbg_f(zd_usb_dev(usb),
1258                         "error: value %#09x has bits >= %d set\n",
1259                         value, bits);
1260                 return -EINVAL;
1261         }
1262 #endif /* DEBUG */
1263
1264         dev_dbg_f(zd_usb_dev(usb), "value %#09x bits %d\n", value, bits);
1265
1266         r = zd_usb_ioread16(usb, &bit_value_template, CR203);
1267         if (r) {
1268                 dev_dbg_f(zd_usb_dev(usb),
1269                         "error %d: Couldn't read CR203\n", r);
1270                 goto out;
1271         }
1272         bit_value_template &= ~(RF_IF_LE|RF_CLK|RF_DATA);
1273
1274         req_len = sizeof(struct usb_req_rfwrite) + bits * sizeof(__le16);
1275         req = kmalloc(req_len, GFP_NOFS);
1276         if (!req)
1277                 return -ENOMEM;
1278
1279         req->id = cpu_to_le16(USB_REQ_WRITE_RF);
1280         /* 1: 3683a, but not used in ZYDAS driver */
1281         req->value = cpu_to_le16(2);
1282         req->bits = cpu_to_le16(bits);
1283
1284         for (i = 0; i < bits; i++) {
1285                 u16 bv = bit_value_template;
1286                 if (value & (1 << (bits-1-i)))
1287                         bv |= RF_DATA;
1288                 req->bit_values[i] = cpu_to_le16(bv);
1289         }
1290
1291         udev = zd_usb_to_usbdev(usb);
1292         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, EP_REGS_OUT),
1293                          req, req_len, &actual_req_len, 1000 /* ms */);
1294         if (r) {
1295                 dev_dbg_f(zd_usb_dev(usb),
1296                         "error in usb_bulk_msg(). Error number %d\n", r);
1297                 goto out;
1298         }
1299         if (req_len != actual_req_len) {
1300                 dev_dbg_f(zd_usb_dev(usb), "error in usb_bulk_msg()"
1301                         " req_len %d != actual_req_len %d\n",
1302                         req_len, actual_req_len);
1303                 r = -EIO;
1304                 goto out;
1305         }
1306
1307         /* FALL-THROUGH with r == 0 */
1308 out:
1309         kfree(req);
1310         return r;
1311 }