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