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