GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / usb / storage / alauda.c
1 /*
2  * Driver for Alauda-based card readers
3  *
4  * Current development and maintenance by:
5  *   (c) 2005 Daniel Drake <dsd@gentoo.org>
6  *
7  * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
8  *
9  * Alauda implements a vendor-specific command set to access two media reader
10  * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
11  * which are accepted by these devices.
12  *
13  * The driver was developed through reverse-engineering, with the help of the
14  * sddr09 driver which has many similarities, and with some help from the
15  * (very old) vendor-supplied GPL sma03 driver.
16  *
17  * For protocol info, see http://alauda.sourceforge.net
18  *
19  * This program is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License as published by the
21  * Free Software Foundation; either version 2, or (at your option) any
22  * later version.
23  *
24  * This program is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  * General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, write to the Free Software Foundation, Inc.,
31  * 675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33
34 #include <linux/module.h>
35 #include <linux/slab.h>
36
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_device.h>
40
41 #include "usb.h"
42 #include "transport.h"
43 #include "protocol.h"
44 #include "debug.h"
45 #include "scsiglue.h"
46
47 #define DRV_NAME "ums-alauda"
48
49 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
50 MODULE_AUTHOR("Daniel Drake <dsd@gentoo.org>");
51 MODULE_LICENSE("GPL");
52
53 /*
54  * Status bytes
55  */
56 #define ALAUDA_STATUS_ERROR             0x01
57 #define ALAUDA_STATUS_READY             0x40
58
59 /*
60  * Control opcodes (for request field)
61  */
62 #define ALAUDA_GET_XD_MEDIA_STATUS      0x08
63 #define ALAUDA_GET_SM_MEDIA_STATUS      0x98
64 #define ALAUDA_ACK_XD_MEDIA_CHANGE      0x0a
65 #define ALAUDA_ACK_SM_MEDIA_CHANGE      0x9a
66 #define ALAUDA_GET_XD_MEDIA_SIG         0x86
67 #define ALAUDA_GET_SM_MEDIA_SIG         0x96
68
69 /*
70  * Bulk command identity (byte 0)
71  */
72 #define ALAUDA_BULK_CMD                 0x40
73
74 /*
75  * Bulk opcodes (byte 1)
76  */
77 #define ALAUDA_BULK_GET_REDU_DATA       0x85
78 #define ALAUDA_BULK_READ_BLOCK          0x94
79 #define ALAUDA_BULK_ERASE_BLOCK         0xa3
80 #define ALAUDA_BULK_WRITE_BLOCK         0xb4
81 #define ALAUDA_BULK_GET_STATUS2         0xb7
82 #define ALAUDA_BULK_RESET_MEDIA         0xe0
83
84 /*
85  * Port to operate on (byte 8)
86  */
87 #define ALAUDA_PORT_XD                  0x00
88 #define ALAUDA_PORT_SM                  0x01
89
90 /*
91  * LBA and PBA are unsigned ints. Special values.
92  */
93 #define UNDEF    0xffff
94 #define SPARE    0xfffe
95 #define UNUSABLE 0xfffd
96
97 struct alauda_media_info {
98         unsigned long capacity;         /* total media size in bytes */
99         unsigned int pagesize;          /* page size in bytes */
100         unsigned int blocksize;         /* number of pages per block */
101         unsigned int uzonesize;         /* number of usable blocks per zone */
102         unsigned int zonesize;          /* number of blocks per zone */
103         unsigned int blockmask;         /* mask to get page from address */
104
105         unsigned char pageshift;
106         unsigned char blockshift;
107         unsigned char zoneshift;
108
109         u16 **lba_to_pba;               /* logical to physical block map */
110         u16 **pba_to_lba;               /* physical to logical block map */
111 };
112
113 struct alauda_info {
114         struct alauda_media_info port[2];
115         int wr_ep;                      /* endpoint to write data out of */
116
117         unsigned char sense_key;
118         unsigned long sense_asc;        /* additional sense code */
119         unsigned long sense_ascq;       /* additional sense code qualifier */
120 };
121
122 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
123 #define LSB_of(s) ((s)&0xFF)
124 #define MSB_of(s) ((s)>>8)
125
126 #define MEDIA_PORT(us) us->srb->device->lun
127 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
128
129 #define PBA_LO(pba) ((pba & 0xF) << 5)
130 #define PBA_HI(pba) (pba >> 3)
131 #define PBA_ZONE(pba) (pba >> 11)
132
133 static int init_alauda(struct us_data *us);
134
135
136 /*
137  * The table of devices
138  */
139 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
140                     vendorName, productName, useProtocol, useTransport, \
141                     initFunction, flags) \
142 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
143   .driver_info = (flags) }
144
145 static struct usb_device_id alauda_usb_ids[] = {
146 #       include "unusual_alauda.h"
147         { }             /* Terminating entry */
148 };
149 MODULE_DEVICE_TABLE(usb, alauda_usb_ids);
150
151 #undef UNUSUAL_DEV
152
153 /*
154  * The flags table
155  */
156 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
157                     vendor_name, product_name, use_protocol, use_transport, \
158                     init_function, Flags) \
159 { \
160         .vendorName = vendor_name,      \
161         .productName = product_name,    \
162         .useProtocol = use_protocol,    \
163         .useTransport = use_transport,  \
164         .initFunction = init_function,  \
165 }
166
167 static struct us_unusual_dev alauda_unusual_dev_list[] = {
168 #       include "unusual_alauda.h"
169         { }             /* Terminating entry */
170 };
171
172 #undef UNUSUAL_DEV
173
174
175 /*
176  * Media handling
177  */
178
179 struct alauda_card_info {
180         unsigned char id;               /* id byte */
181         unsigned char chipshift;        /* 1<<cs bytes total capacity */
182         unsigned char pageshift;        /* 1<<ps bytes in a page */
183         unsigned char blockshift;       /* 1<<bs pages per block */
184         unsigned char zoneshift;        /* 1<<zs blocks per zone */
185 };
186
187 static struct alauda_card_info alauda_card_ids[] = {
188         /* NAND flash */
189         { 0x6e, 20, 8, 4, 8},   /* 1 MB */
190         { 0xe8, 20, 8, 4, 8},   /* 1 MB */
191         { 0xec, 20, 8, 4, 8},   /* 1 MB */
192         { 0x64, 21, 8, 4, 9},   /* 2 MB */
193         { 0xea, 21, 8, 4, 9},   /* 2 MB */
194         { 0x6b, 22, 9, 4, 9},   /* 4 MB */
195         { 0xe3, 22, 9, 4, 9},   /* 4 MB */
196         { 0xe5, 22, 9, 4, 9},   /* 4 MB */
197         { 0xe6, 23, 9, 4, 10},  /* 8 MB */
198         { 0x73, 24, 9, 5, 10},  /* 16 MB */
199         { 0x75, 25, 9, 5, 10},  /* 32 MB */
200         { 0x76, 26, 9, 5, 10},  /* 64 MB */
201         { 0x79, 27, 9, 5, 10},  /* 128 MB */
202         { 0x71, 28, 9, 5, 10},  /* 256 MB */
203
204         /* MASK ROM */
205         { 0x5d, 21, 9, 4, 8},   /* 2 MB */
206         { 0xd5, 22, 9, 4, 9},   /* 4 MB */
207         { 0xd6, 23, 9, 4, 10},  /* 8 MB */
208         { 0x57, 24, 9, 4, 11},  /* 16 MB */
209         { 0x58, 25, 9, 4, 12},  /* 32 MB */
210         { 0,}
211 };
212
213 static struct alauda_card_info *alauda_card_find_id(unsigned char id)
214 {
215         int i;
216
217         for (i = 0; alauda_card_ids[i].id != 0; i++)
218                 if (alauda_card_ids[i].id == id)
219                         return &(alauda_card_ids[i]);
220         return NULL;
221 }
222
223 /*
224  * ECC computation.
225  */
226
227 static unsigned char parity[256];
228 static unsigned char ecc2[256];
229
230 static void nand_init_ecc(void)
231 {
232         int i, j, a;
233
234         parity[0] = 0;
235         for (i = 1; i < 256; i++)
236                 parity[i] = (parity[i&(i-1)] ^ 1);
237
238         for (i = 0; i < 256; i++) {
239                 a = 0;
240                 for (j = 0; j < 8; j++) {
241                         if (i & (1<<j)) {
242                                 if ((j & 1) == 0)
243                                         a ^= 0x04;
244                                 if ((j & 2) == 0)
245                                         a ^= 0x10;
246                                 if ((j & 4) == 0)
247                                         a ^= 0x40;
248                         }
249                 }
250                 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
251         }
252 }
253
254 /* compute 3-byte ecc on 256 bytes */
255 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc)
256 {
257         int i, j, a;
258         unsigned char par = 0, bit, bits[8] = {0};
259
260         /* collect 16 checksum bits */
261         for (i = 0; i < 256; i++) {
262                 par ^= data[i];
263                 bit = parity[data[i]];
264                 for (j = 0; j < 8; j++)
265                         if ((i & (1<<j)) == 0)
266                                 bits[j] ^= bit;
267         }
268
269         /* put 4+4+4 = 12 bits in the ecc */
270         a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
271         ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
272
273         a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
274         ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
275
276         ecc[2] = ecc2[par];
277 }
278
279 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc)
280 {
281         return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
282 }
283
284 static void nand_store_ecc(unsigned char *data, unsigned char *ecc)
285 {
286         memcpy(data, ecc, 3);
287 }
288
289 /*
290  * Alauda driver
291  */
292
293 /*
294  * Forget our PBA <---> LBA mappings for a particular port
295  */
296 static void alauda_free_maps (struct alauda_media_info *media_info)
297 {
298         unsigned int shift = media_info->zoneshift
299                 + media_info->blockshift + media_info->pageshift;
300         unsigned int num_zones = media_info->capacity >> shift;
301         unsigned int i;
302
303         if (media_info->lba_to_pba != NULL)
304                 for (i = 0; i < num_zones; i++) {
305                         kfree(media_info->lba_to_pba[i]);
306                         media_info->lba_to_pba[i] = NULL;
307                 }
308
309         if (media_info->pba_to_lba != NULL)
310                 for (i = 0; i < num_zones; i++) {
311                         kfree(media_info->pba_to_lba[i]);
312                         media_info->pba_to_lba[i] = NULL;
313                 }
314 }
315
316 /*
317  * Returns 2 bytes of status data
318  * The first byte describes media status, and second byte describes door status
319  */
320 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
321 {
322         int rc;
323         unsigned char command;
324
325         if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
326                 command = ALAUDA_GET_XD_MEDIA_STATUS;
327         else
328                 command = ALAUDA_GET_SM_MEDIA_STATUS;
329
330         rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
331                 command, 0xc0, 0, 1, data, 2);
332
333         if (rc == USB_STOR_XFER_GOOD)
334                 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]);
335
336         return rc;
337 }
338
339 /*
340  * Clears the "media was changed" bit so that we know when it changes again
341  * in the future.
342  */
343 static int alauda_ack_media(struct us_data *us)
344 {
345         unsigned char command;
346
347         if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
348                 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
349         else
350                 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
351
352         return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
353                 command, 0x40, 0, 1, NULL, 0);
354 }
355
356 /*
357  * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
358  * and some other details.
359  */
360 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
361 {
362         unsigned char command;
363
364         if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
365                 command = ALAUDA_GET_XD_MEDIA_SIG;
366         else
367                 command = ALAUDA_GET_SM_MEDIA_SIG;
368
369         return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
370                 command, 0xc0, 0, 0, data, 4);
371 }
372
373 /*
374  * Resets the media status (but not the whole device?)
375  */
376 static int alauda_reset_media(struct us_data *us)
377 {
378         unsigned char *command = us->iobuf;
379
380         memset(command, 0, 9);
381         command[0] = ALAUDA_BULK_CMD;
382         command[1] = ALAUDA_BULK_RESET_MEDIA;
383         command[8] = MEDIA_PORT(us);
384
385         return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
386                 command, 9, NULL);
387 }
388
389 /*
390  * Examines the media and deduces capacity, etc.
391  */
392 static int alauda_init_media(struct us_data *us)
393 {
394         unsigned char *data = us->iobuf;
395         int ready = 0;
396         struct alauda_card_info *media_info;
397         unsigned int num_zones;
398
399         while (ready == 0) {
400                 msleep(20);
401
402                 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
403                         return USB_STOR_TRANSPORT_ERROR;
404
405                 if (data[0] & 0x10)
406                         ready = 1;
407         }
408
409         usb_stor_dbg(us, "We are ready for action!\n");
410
411         if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
412                 return USB_STOR_TRANSPORT_ERROR;
413
414         msleep(10);
415
416         if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
417                 return USB_STOR_TRANSPORT_ERROR;
418
419         if (data[0] != 0x14) {
420                 usb_stor_dbg(us, "Media not ready after ack\n");
421                 return USB_STOR_TRANSPORT_ERROR;
422         }
423
424         if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
425                 return USB_STOR_TRANSPORT_ERROR;
426
427         usb_stor_dbg(us, "Media signature: %4ph\n", data);
428         media_info = alauda_card_find_id(data[1]);
429         if (media_info == NULL) {
430                 pr_warn("alauda_init_media: Unrecognised media signature: %4ph\n",
431                         data);
432                 return USB_STOR_TRANSPORT_ERROR;
433         }
434
435         MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
436         usb_stor_dbg(us, "Found media with capacity: %ldMB\n",
437                      MEDIA_INFO(us).capacity >> 20);
438
439         MEDIA_INFO(us).pageshift = media_info->pageshift;
440         MEDIA_INFO(us).blockshift = media_info->blockshift;
441         MEDIA_INFO(us).zoneshift = media_info->zoneshift;
442
443         MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
444         MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
445         MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
446
447         MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
448         MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
449
450         num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
451                 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
452         MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
453         MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
454         if (MEDIA_INFO(us).pba_to_lba == NULL || MEDIA_INFO(us).lba_to_pba == NULL)
455                 return USB_STOR_TRANSPORT_ERROR;
456
457         if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
458                 return USB_STOR_TRANSPORT_ERROR;
459
460         return USB_STOR_TRANSPORT_GOOD;
461 }
462
463 /*
464  * Examines the media status and does the right thing when the media has gone,
465  * appeared, or changed.
466  */
467 static int alauda_check_media(struct us_data *us)
468 {
469         struct alauda_info *info = (struct alauda_info *) us->extra;
470         unsigned char *status = us->iobuf;
471         int rc;
472
473         rc = alauda_get_media_status(us, status);
474         if (rc != USB_STOR_XFER_GOOD) {
475                 status[0] = 0xF0;       /* Pretend there's no media */
476                 status[1] = 0;
477         }
478
479         /* Check for no media or door open */
480         if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
481                 || ((status[1] & 0x01) == 0)) {
482                 usb_stor_dbg(us, "No media, or door open\n");
483                 alauda_free_maps(&MEDIA_INFO(us));
484                 info->sense_key = 0x02;
485                 info->sense_asc = 0x3A;
486                 info->sense_ascq = 0x00;
487                 return USB_STOR_TRANSPORT_FAILED;
488         }
489
490         /* Check for media change */
491         if (status[0] & 0x08) {
492                 usb_stor_dbg(us, "Media change detected\n");
493                 alauda_free_maps(&MEDIA_INFO(us));
494                 alauda_init_media(us);
495
496                 info->sense_key = UNIT_ATTENTION;
497                 info->sense_asc = 0x28;
498                 info->sense_ascq = 0x00;
499                 return USB_STOR_TRANSPORT_FAILED;
500         }
501
502         return USB_STOR_TRANSPORT_GOOD;
503 }
504
505 /*
506  * Checks the status from the 2nd status register
507  * Returns 3 bytes of status data, only the first is known
508  */
509 static int alauda_check_status2(struct us_data *us)
510 {
511         int rc;
512         unsigned char command[] = {
513                 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
514                 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
515         };
516         unsigned char data[3];
517
518         rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
519                 command, 9, NULL);
520         if (rc != USB_STOR_XFER_GOOD)
521                 return rc;
522
523         rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
524                 data, 3, NULL);
525         if (rc != USB_STOR_XFER_GOOD)
526                 return rc;
527
528         usb_stor_dbg(us, "%3ph\n", data);
529         if (data[0] & ALAUDA_STATUS_ERROR)
530                 return USB_STOR_XFER_ERROR;
531
532         return USB_STOR_XFER_GOOD;
533 }
534
535 /*
536  * Gets the redundancy data for the first page of a PBA
537  * Returns 16 bytes.
538  */
539 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
540 {
541         int rc;
542         unsigned char command[] = {
543                 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
544                 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
545         };
546
547         rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
548                 command, 9, NULL);
549         if (rc != USB_STOR_XFER_GOOD)
550                 return rc;
551
552         return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
553                 data, 16, NULL);
554 }
555
556 /*
557  * Finds the first unused PBA in a zone
558  * Returns the absolute PBA of an unused PBA, or 0 if none found.
559  */
560 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
561         unsigned int zone)
562 {
563         u16 *pba_to_lba = info->pba_to_lba[zone];
564         unsigned int i;
565
566         for (i = 0; i < info->zonesize; i++)
567                 if (pba_to_lba[i] == UNDEF)
568                         return (zone << info->zoneshift) + i;
569
570         return 0;
571 }
572
573 /*
574  * Reads the redundancy data for all PBA's in a zone
575  * Produces lba <--> pba mappings
576  */
577 static int alauda_read_map(struct us_data *us, unsigned int zone)
578 {
579         unsigned char *data = us->iobuf;
580         int result;
581         int i, j;
582         unsigned int zonesize = MEDIA_INFO(us).zonesize;
583         unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
584         unsigned int lba_offset, lba_real, blocknum;
585         unsigned int zone_base_lba = zone * uzonesize;
586         unsigned int zone_base_pba = zone * zonesize;
587         u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
588         u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
589         if (lba_to_pba == NULL || pba_to_lba == NULL) {
590                 result = USB_STOR_TRANSPORT_ERROR;
591                 goto error;
592         }
593
594         usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone);
595
596         /* 1024 PBA's per zone */
597         for (i = 0; i < zonesize; i++)
598                 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
599
600         for (i = 0; i < zonesize; i++) {
601                 blocknum = zone_base_pba + i;
602
603                 result = alauda_get_redu_data(us, blocknum, data);
604                 if (result != USB_STOR_XFER_GOOD) {
605                         result = USB_STOR_TRANSPORT_ERROR;
606                         goto error;
607                 }
608
609                 /* special PBAs have control field 0^16 */
610                 for (j = 0; j < 16; j++)
611                         if (data[j] != 0)
612                                 goto nonz;
613                 pba_to_lba[i] = UNUSABLE;
614                 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum);
615                 continue;
616
617         nonz:
618                 /* unwritten PBAs have control field FF^16 */
619                 for (j = 0; j < 16; j++)
620                         if (data[j] != 0xff)
621                                 goto nonff;
622                 continue;
623
624         nonff:
625                 /* normal PBAs start with six FFs */
626                 if (j < 6) {
627                         usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X block status %02X\n",
628                                      blocknum,
629                                      data[0], data[1], data[2], data[3],
630                                      data[4], data[5]);
631                         pba_to_lba[i] = UNUSABLE;
632                         continue;
633                 }
634
635                 if ((data[6] >> 4) != 0x01) {
636                         usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n",
637                                      blocknum, data[6], data[7],
638                                      data[11], data[12]);
639                         pba_to_lba[i] = UNUSABLE;
640                         continue;
641                 }
642
643                 /* check even parity */
644                 if (parity[data[6] ^ data[7]]) {
645                         printk(KERN_WARNING
646                                "alauda_read_map: Bad parity in LBA for block %d"
647                                " (%02X %02X)\n", i, data[6], data[7]);
648                         pba_to_lba[i] = UNUSABLE;
649                         continue;
650                 }
651
652                 lba_offset = short_pack(data[7], data[6]);
653                 lba_offset = (lba_offset & 0x07FF) >> 1;
654                 lba_real = lba_offset + zone_base_lba;
655
656                 /*
657                  * Every 1024 physical blocks ("zone"), the LBA numbers
658                  * go back to zero, but are within a higher block of LBA's.
659                  * Also, there is a maximum of 1000 LBA's per zone.
660                  * In other words, in PBA 1024-2047 you will find LBA 0-999
661                  * which are really LBA 1000-1999. This allows for 24 bad
662                  * or special physical blocks per zone.
663                  */
664
665                 if (lba_offset >= uzonesize) {
666                         printk(KERN_WARNING
667                                "alauda_read_map: Bad low LBA %d for block %d\n",
668                                lba_real, blocknum);
669                         continue;
670                 }
671
672                 if (lba_to_pba[lba_offset] != UNDEF) {
673                         printk(KERN_WARNING
674                                "alauda_read_map: "
675                                "LBA %d seen for PBA %d and %d\n",
676                                lba_real, lba_to_pba[lba_offset], blocknum);
677                         continue;
678                 }
679
680                 pba_to_lba[i] = lba_real;
681                 lba_to_pba[lba_offset] = blocknum;
682                 continue;
683         }
684
685         MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
686         MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
687         result = 0;
688         goto out;
689
690 error:
691         kfree(lba_to_pba);
692         kfree(pba_to_lba);
693 out:
694         return result;
695 }
696
697 /*
698  * Checks to see whether we have already mapped a certain zone
699  * If we haven't, the map is generated
700  */
701 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
702 {
703         if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
704                 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
705                 alauda_read_map(us, zone);
706 }
707
708 /*
709  * Erases an entire block
710  */
711 static int alauda_erase_block(struct us_data *us, u16 pba)
712 {
713         int rc;
714         unsigned char command[] = {
715                 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
716                 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
717         };
718         unsigned char buf[2];
719
720         usb_stor_dbg(us, "Erasing PBA %d\n", pba);
721
722         rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
723                 command, 9, NULL);
724         if (rc != USB_STOR_XFER_GOOD)
725                 return rc;
726
727         rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
728                 buf, 2, NULL);
729         if (rc != USB_STOR_XFER_GOOD)
730                 return rc;
731
732         usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]);
733         return rc;
734 }
735
736 /*
737  * Reads data from a certain offset page inside a PBA, including interleaved
738  * redundancy data. Returns (pagesize+64)*pages bytes in data.
739  */
740 static int alauda_read_block_raw(struct us_data *us, u16 pba,
741                 unsigned int page, unsigned int pages, unsigned char *data)
742 {
743         int rc;
744         unsigned char command[] = {
745                 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
746                 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
747         };
748
749         usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages);
750
751         rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
752                 command, 9, NULL);
753         if (rc != USB_STOR_XFER_GOOD)
754                 return rc;
755
756         return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
757                 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
758 }
759
760 /*
761  * Reads data from a certain offset page inside a PBA, excluding redundancy
762  * data. Returns pagesize*pages bytes in data. Note that data must be big enough
763  * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
764  * trailing bytes outside this function.
765  */
766 static int alauda_read_block(struct us_data *us, u16 pba,
767                 unsigned int page, unsigned int pages, unsigned char *data)
768 {
769         int i, rc;
770         unsigned int pagesize = MEDIA_INFO(us).pagesize;
771
772         rc = alauda_read_block_raw(us, pba, page, pages, data);
773         if (rc != USB_STOR_XFER_GOOD)
774                 return rc;
775
776         /* Cut out the redundancy data */
777         for (i = 0; i < pages; i++) {
778                 int dest_offset = i * pagesize;
779                 int src_offset = i * (pagesize + 64);
780                 memmove(data + dest_offset, data + src_offset, pagesize);
781         }
782
783         return rc;
784 }
785
786 /*
787  * Writes an entire block of data and checks status after write.
788  * Redundancy data must be already included in data. Data should be
789  * (pagesize+64)*blocksize bytes in length.
790  */
791 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
792 {
793         int rc;
794         struct alauda_info *info = (struct alauda_info *) us->extra;
795         unsigned char command[] = {
796                 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
797                 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
798         };
799
800         usb_stor_dbg(us, "pba %d\n", pba);
801
802         rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
803                 command, 9, NULL);
804         if (rc != USB_STOR_XFER_GOOD)
805                 return rc;
806
807         rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
808                 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
809                 NULL);
810         if (rc != USB_STOR_XFER_GOOD)
811                 return rc;
812
813         return alauda_check_status2(us);
814 }
815
816 /*
817  * Write some data to a specific LBA.
818  */
819 static int alauda_write_lba(struct us_data *us, u16 lba,
820                  unsigned int page, unsigned int pages,
821                  unsigned char *ptr, unsigned char *blockbuffer)
822 {
823         u16 pba, lbap, new_pba;
824         unsigned char *bptr, *cptr, *xptr;
825         unsigned char ecc[3];
826         int i, result;
827         unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
828         unsigned int zonesize = MEDIA_INFO(us).zonesize;
829         unsigned int pagesize = MEDIA_INFO(us).pagesize;
830         unsigned int blocksize = MEDIA_INFO(us).blocksize;
831         unsigned int lba_offset = lba % uzonesize;
832         unsigned int new_pba_offset;
833         unsigned int zone = lba / uzonesize;
834
835         alauda_ensure_map_for_zone(us, zone);
836
837         pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
838         if (pba == 1) {
839                 /*
840                  * Maybe it is impossible to write to PBA 1.
841                  * Fake success, but don't do anything.
842                  */
843                 printk(KERN_WARNING
844                        "alauda_write_lba: avoid writing to pba 1\n");
845                 return USB_STOR_TRANSPORT_GOOD;
846         }
847
848         new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
849         if (!new_pba) {
850                 printk(KERN_WARNING
851                        "alauda_write_lba: Out of unused blocks\n");
852                 return USB_STOR_TRANSPORT_ERROR;
853         }
854
855         /* read old contents */
856         if (pba != UNDEF) {
857                 result = alauda_read_block_raw(us, pba, 0,
858                         blocksize, blockbuffer);
859                 if (result != USB_STOR_XFER_GOOD)
860                         return result;
861         } else {
862                 memset(blockbuffer, 0, blocksize * (pagesize + 64));
863         }
864
865         lbap = (lba_offset << 1) | 0x1000;
866         if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
867                 lbap ^= 1;
868
869         /* check old contents and fill lba */
870         for (i = 0; i < blocksize; i++) {
871                 bptr = blockbuffer + (i * (pagesize + 64));
872                 cptr = bptr + pagesize;
873                 nand_compute_ecc(bptr, ecc);
874                 if (!nand_compare_ecc(cptr+13, ecc)) {
875                         usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n",
876                                      i, pba);
877                         nand_store_ecc(cptr+13, ecc);
878                 }
879                 nand_compute_ecc(bptr + (pagesize / 2), ecc);
880                 if (!nand_compare_ecc(cptr+8, ecc)) {
881                         usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n",
882                                      i, pba);
883                         nand_store_ecc(cptr+8, ecc);
884                 }
885                 cptr[6] = cptr[11] = MSB_of(lbap);
886                 cptr[7] = cptr[12] = LSB_of(lbap);
887         }
888
889         /* copy in new stuff and compute ECC */
890         xptr = ptr;
891         for (i = page; i < page+pages; i++) {
892                 bptr = blockbuffer + (i * (pagesize + 64));
893                 cptr = bptr + pagesize;
894                 memcpy(bptr, xptr, pagesize);
895                 xptr += pagesize;
896                 nand_compute_ecc(bptr, ecc);
897                 nand_store_ecc(cptr+13, ecc);
898                 nand_compute_ecc(bptr + (pagesize / 2), ecc);
899                 nand_store_ecc(cptr+8, ecc);
900         }
901
902         result = alauda_write_block(us, new_pba, blockbuffer);
903         if (result != USB_STOR_XFER_GOOD)
904                 return result;
905
906         new_pba_offset = new_pba - (zone * zonesize);
907         MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
908         MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
909         usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba);
910
911         if (pba != UNDEF) {
912                 unsigned int pba_offset = pba - (zone * zonesize);
913                 result = alauda_erase_block(us, pba);
914                 if (result != USB_STOR_XFER_GOOD)
915                         return result;
916                 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
917         }
918
919         return USB_STOR_TRANSPORT_GOOD;
920 }
921
922 /*
923  * Read data from a specific sector address
924  */
925 static int alauda_read_data(struct us_data *us, unsigned long address,
926                 unsigned int sectors)
927 {
928         unsigned char *buffer;
929         u16 lba, max_lba;
930         unsigned int page, len, offset;
931         unsigned int blockshift = MEDIA_INFO(us).blockshift;
932         unsigned int pageshift = MEDIA_INFO(us).pageshift;
933         unsigned int blocksize = MEDIA_INFO(us).blocksize;
934         unsigned int pagesize = MEDIA_INFO(us).pagesize;
935         unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
936         struct scatterlist *sg;
937         int result;
938
939         /*
940          * Since we only read in one block at a time, we have to create
941          * a bounce buffer and move the data a piece at a time between the
942          * bounce buffer and the actual transfer buffer.
943          * We make this buffer big enough to hold temporary redundancy data,
944          * which we use when reading the data blocks.
945          */
946
947         len = min(sectors, blocksize) * (pagesize + 64);
948         buffer = kmalloc(len, GFP_NOIO);
949         if (!buffer)
950                 return USB_STOR_TRANSPORT_ERROR;
951
952         /* Figure out the initial LBA and page */
953         lba = address >> blockshift;
954         page = (address & MEDIA_INFO(us).blockmask);
955         max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
956
957         result = USB_STOR_TRANSPORT_GOOD;
958         offset = 0;
959         sg = NULL;
960
961         while (sectors > 0) {
962                 unsigned int zone = lba / uzonesize; /* integer division */
963                 unsigned int lba_offset = lba - (zone * uzonesize);
964                 unsigned int pages;
965                 u16 pba;
966                 alauda_ensure_map_for_zone(us, zone);
967
968                 /* Not overflowing capacity? */
969                 if (lba >= max_lba) {
970                         usb_stor_dbg(us, "Error: Requested lba %u exceeds maximum %u\n",
971                                      lba, max_lba);
972                         result = USB_STOR_TRANSPORT_ERROR;
973                         break;
974                 }
975
976                 /* Find number of pages we can read in this block */
977                 pages = min(sectors, blocksize - page);
978                 len = pages << pageshift;
979
980                 /* Find where this lba lives on disk */
981                 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
982
983                 if (pba == UNDEF) {     /* this lba was never written */
984                         usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n",
985                                      pages, lba, page);
986
987                         /*
988                          * This is not really an error. It just means
989                          * that the block has never been written.
990                          * Instead of returning USB_STOR_TRANSPORT_ERROR
991                          * it is better to return all zero data.
992                          */
993
994                         memset(buffer, 0, len);
995                 } else {
996                         usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n",
997                                      pages, pba, lba, page);
998
999                         result = alauda_read_block(us, pba, page, pages, buffer);
1000                         if (result != USB_STOR_TRANSPORT_GOOD)
1001                                 break;
1002                 }
1003
1004                 /* Store the data in the transfer buffer */
1005                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1006                                 &sg, &offset, TO_XFER_BUF);
1007
1008                 page = 0;
1009                 lba++;
1010                 sectors -= pages;
1011         }
1012
1013         kfree(buffer);
1014         return result;
1015 }
1016
1017 /*
1018  * Write data to a specific sector address
1019  */
1020 static int alauda_write_data(struct us_data *us, unsigned long address,
1021                 unsigned int sectors)
1022 {
1023         unsigned char *buffer, *blockbuffer;
1024         unsigned int page, len, offset;
1025         unsigned int blockshift = MEDIA_INFO(us).blockshift;
1026         unsigned int pageshift = MEDIA_INFO(us).pageshift;
1027         unsigned int blocksize = MEDIA_INFO(us).blocksize;
1028         unsigned int pagesize = MEDIA_INFO(us).pagesize;
1029         struct scatterlist *sg;
1030         u16 lba, max_lba;
1031         int result;
1032
1033         /*
1034          * Since we don't write the user data directly to the device,
1035          * we have to create a bounce buffer and move the data a piece
1036          * at a time between the bounce buffer and the actual transfer buffer.
1037          */
1038
1039         len = min(sectors, blocksize) * pagesize;
1040         buffer = kmalloc(len, GFP_NOIO);
1041         if (!buffer)
1042                 return USB_STOR_TRANSPORT_ERROR;
1043
1044         /*
1045          * We also need a temporary block buffer, where we read in the old data,
1046          * overwrite parts with the new data, and manipulate the redundancy data
1047          */
1048         blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
1049         if (!blockbuffer) {
1050                 kfree(buffer);
1051                 return USB_STOR_TRANSPORT_ERROR;
1052         }
1053
1054         /* Figure out the initial LBA and page */
1055         lba = address >> blockshift;
1056         page = (address & MEDIA_INFO(us).blockmask);
1057         max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1058
1059         result = USB_STOR_TRANSPORT_GOOD;
1060         offset = 0;
1061         sg = NULL;
1062
1063         while (sectors > 0) {
1064                 /* Write as many sectors as possible in this block */
1065                 unsigned int pages = min(sectors, blocksize - page);
1066                 len = pages << pageshift;
1067
1068                 /* Not overflowing capacity? */
1069                 if (lba >= max_lba) {
1070                         usb_stor_dbg(us, "Requested lba %u exceeds maximum %u\n",
1071                                      lba, max_lba);
1072                         result = USB_STOR_TRANSPORT_ERROR;
1073                         break;
1074                 }
1075
1076                 /* Get the data from the transfer buffer */
1077                 usb_stor_access_xfer_buf(buffer, len, us->srb,
1078                                 &sg, &offset, FROM_XFER_BUF);
1079
1080                 result = alauda_write_lba(us, lba, page, pages, buffer,
1081                         blockbuffer);
1082                 if (result != USB_STOR_TRANSPORT_GOOD)
1083                         break;
1084
1085                 page = 0;
1086                 lba++;
1087                 sectors -= pages;
1088         }
1089
1090         kfree(buffer);
1091         kfree(blockbuffer);
1092         return result;
1093 }
1094
1095 /*
1096  * Our interface with the rest of the world
1097  */
1098
1099 static void alauda_info_destructor(void *extra)
1100 {
1101         struct alauda_info *info = (struct alauda_info *) extra;
1102         int port;
1103
1104         if (!info)
1105                 return;
1106
1107         for (port = 0; port < 2; port++) {
1108                 struct alauda_media_info *media_info = &info->port[port];
1109
1110                 alauda_free_maps(media_info);
1111                 kfree(media_info->lba_to_pba);
1112                 kfree(media_info->pba_to_lba);
1113         }
1114 }
1115
1116 /*
1117  * Initialize alauda_info struct and find the data-write endpoint
1118  */
1119 static int init_alauda(struct us_data *us)
1120 {
1121         struct alauda_info *info;
1122         struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1123         nand_init_ecc();
1124
1125         us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1126         if (!us->extra)
1127                 return USB_STOR_TRANSPORT_ERROR;
1128
1129         info = (struct alauda_info *) us->extra;
1130         us->extra_destructor = alauda_info_destructor;
1131
1132         info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1133                 altsetting->endpoint[0].desc.bEndpointAddress
1134                 & USB_ENDPOINT_NUMBER_MASK);
1135
1136         return USB_STOR_TRANSPORT_GOOD;
1137 }
1138
1139 static int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1140 {
1141         int rc;
1142         struct alauda_info *info = (struct alauda_info *) us->extra;
1143         unsigned char *ptr = us->iobuf;
1144         static unsigned char inquiry_response[36] = {
1145                 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1146         };
1147
1148         if (srb->cmnd[0] == INQUIRY) {
1149                 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n");
1150                 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1151                 fill_inquiry_response(us, ptr, 36);
1152                 return USB_STOR_TRANSPORT_GOOD;
1153         }
1154
1155         if (srb->cmnd[0] == TEST_UNIT_READY) {
1156                 usb_stor_dbg(us, "TEST_UNIT_READY\n");
1157                 return alauda_check_media(us);
1158         }
1159
1160         if (srb->cmnd[0] == READ_CAPACITY) {
1161                 unsigned int num_zones;
1162                 unsigned long capacity;
1163
1164                 rc = alauda_check_media(us);
1165                 if (rc != USB_STOR_TRANSPORT_GOOD)
1166                         return rc;
1167
1168                 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1169                         + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1170
1171                 capacity = num_zones * MEDIA_INFO(us).uzonesize
1172                         * MEDIA_INFO(us).blocksize;
1173
1174                 /* Report capacity and page size */
1175                 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1176                 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1177
1178                 usb_stor_set_xfer_buf(ptr, 8, srb);
1179                 return USB_STOR_TRANSPORT_GOOD;
1180         }
1181
1182         if (srb->cmnd[0] == READ_10) {
1183                 unsigned int page, pages;
1184
1185                 rc = alauda_check_media(us);
1186                 if (rc != USB_STOR_TRANSPORT_GOOD)
1187                         return rc;
1188
1189                 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1190                 page <<= 16;
1191                 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1192                 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1193
1194                 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages);
1195
1196                 return alauda_read_data(us, page, pages);
1197         }
1198
1199         if (srb->cmnd[0] == WRITE_10) {
1200                 unsigned int page, pages;
1201
1202                 rc = alauda_check_media(us);
1203                 if (rc != USB_STOR_TRANSPORT_GOOD)
1204                         return rc;
1205
1206                 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1207                 page <<= 16;
1208                 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1209                 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1210
1211                 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages);
1212
1213                 return alauda_write_data(us, page, pages);
1214         }
1215
1216         if (srb->cmnd[0] == REQUEST_SENSE) {
1217                 usb_stor_dbg(us, "REQUEST_SENSE\n");
1218
1219                 memset(ptr, 0, 18);
1220                 ptr[0] = 0xF0;
1221                 ptr[2] = info->sense_key;
1222                 ptr[7] = 11;
1223                 ptr[12] = info->sense_asc;
1224                 ptr[13] = info->sense_ascq;
1225                 usb_stor_set_xfer_buf(ptr, 18, srb);
1226
1227                 return USB_STOR_TRANSPORT_GOOD;
1228         }
1229
1230         if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1231                 /*
1232                  * sure.  whatever.  not like we can stop the user from popping
1233                  * the media out of the device (no locking doors, etc)
1234                  */
1235                 return USB_STOR_TRANSPORT_GOOD;
1236         }
1237
1238         usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n",
1239                      srb->cmnd[0], srb->cmnd[0]);
1240         info->sense_key = 0x05;
1241         info->sense_asc = 0x20;
1242         info->sense_ascq = 0x00;
1243         return USB_STOR_TRANSPORT_FAILED;
1244 }
1245
1246 static struct scsi_host_template alauda_host_template;
1247
1248 static int alauda_probe(struct usb_interface *intf,
1249                          const struct usb_device_id *id)
1250 {
1251         struct us_data *us;
1252         int result;
1253
1254         result = usb_stor_probe1(&us, intf, id,
1255                         (id - alauda_usb_ids) + alauda_unusual_dev_list,
1256                         &alauda_host_template);
1257         if (result)
1258                 return result;
1259
1260         us->transport_name  = "Alauda Control/Bulk";
1261         us->transport = alauda_transport;
1262         us->transport_reset = usb_stor_Bulk_reset;
1263         us->max_lun = 1;
1264
1265         result = usb_stor_probe2(us);
1266         return result;
1267 }
1268
1269 static struct usb_driver alauda_driver = {
1270         .name =         DRV_NAME,
1271         .probe =        alauda_probe,
1272         .disconnect =   usb_stor_disconnect,
1273         .suspend =      usb_stor_suspend,
1274         .resume =       usb_stor_resume,
1275         .reset_resume = usb_stor_reset_resume,
1276         .pre_reset =    usb_stor_pre_reset,
1277         .post_reset =   usb_stor_post_reset,
1278         .id_table =     alauda_usb_ids,
1279         .soft_unbind =  1,
1280         .no_dynamic_id = 1,
1281 };
1282
1283 module_usb_stor_driver(alauda_driver, alauda_host_template, DRV_NAME);