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