13 #include <linux/kernel.h>
14 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
25 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
26 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
27 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
30 #define ALAUDA_BULK_CMD 0x40
33 #define ALAUDA_PORT_XD 0x00
34 #define ALAUDA_PORT_SM 0x01
37 #define ALAUDA_BULK_READ_PAGE 0x84
38 #define ALAUDA_BULK_READ_OOB 0x85
39 #define ALAUDA_BULK_READ_BLOCK 0x94
40 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
41 #define ALAUDA_BULK_WRITE_PAGE 0xa4
42 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
43 #define ALAUDA_BULK_RESET_MEDIA 0xe0
46 #define PBA_LO(pba) ((pba & 0xF) << 5)
47 #define PBA_HI(pba) (pba >> 3)
48 #define PBA_ZONE(pba) (pba >> 11)
53 { USB_DEVICE(0x0584, 0x0008) },
54 { USB_DEVICE(0x07b4, 0x010a) },
67 struct usb_device *
dev;
112 for (card = alauda_card_ids; card->
id; card++)
118 static void alauda_delete(
struct kref *
kref)
130 static int alauda_get_media_status(
struct alauda *al,
void *
buf)
141 static int alauda_ack_media(
struct alauda *al)
152 static int alauda_get_media_signatures(
struct alauda *al,
void *buf)
163 static void alauda_reset(
struct alauda *al)
174 static void correct_data(
void *buf,
void *read_ecc,
175 int *corrected,
int *uncorrected)
195 static void alauda_complete(
struct urb *
urb)
203 static int __alauda_read_page(
struct mtd_info *mtd, loff_t
from,
void *buf,
208 u32 pba = from >> al->
card->blockshift;
220 for (i=0; i<3; i++) {
225 init_completion(&
sg.comp);
227 alauda_complete,
NULL);
229 alauda_complete,
NULL);
230 usb_fill_bulk_urb(
sg.urb[2], al->
dev, al->
bulk_in, oob, 16,
231 alauda_complete, &
sg.comp);
234 for (i=0; i<3; i++) {
242 for (i=0; i<3; i++) {
255 static int alauda_read_page(
struct mtd_info *mtd, loff_t from,
256 void *buf,
u8 *oob,
int *corrected,
int *uncorrected)
260 err = __alauda_read_page(mtd, from, buf, oob);
263 correct_data(buf, oob+13, corrected, uncorrected);
264 correct_data(buf+256, oob+8, corrected, uncorrected);
268 static int alauda_write_page(
struct mtd_info *mtd, loff_t to,
void *buf,
273 u32 pba = to >> al->
card->blockshift;
285 for (i=0; i<3; i++) {
290 init_completion(&
sg.comp);
292 alauda_complete,
NULL);
294 alauda_complete,
NULL);
296 alauda_complete, &
sg.comp);
299 for (i=0; i<3; i++) {
307 for (i=0; i<3; i++) {
320 static int alauda_erase_block(
struct mtd_info *mtd, loff_t ofs)
324 u32 pba = ofs >> al->
card->blockshift;
336 for (i=0; i<2; i++) {
341 init_completion(&
sg.comp);
342 usb_fill_bulk_urb(
sg.urb[0], al->
dev, al->
bulk_out, command, 9,
343 alauda_complete,
NULL);
344 usb_fill_bulk_urb(
sg.urb[1], al->
dev, al->
bulk_in, buf, 2,
345 alauda_complete, &
sg.comp);
348 for (i=0; i<2; i++) {
356 for (i=0; i<2; i++) {
368 static int alauda_read_oob(
struct mtd_info *mtd, loff_t from,
void *oob)
370 static u8 ignore_buf[512];
372 return __alauda_read_page(mtd, from, ignore_buf, oob);
375 static int alauda_isbad(
struct mtd_info *mtd, loff_t ofs)
380 err = alauda_read_oob(mtd, ofs, oob);
385 return hweight8(oob[5]) >= 7 ? 0 : 1;
388 static int alauda_bounce_read(
struct mtd_info *mtd, loff_t from,
size_t len,
389 size_t *retlen,
u_char *buf)
393 int err, corrected=0, uncorrected=0;
405 err = alauda_read_page(mtd, from, bounce_buf, oob,
406 &corrected, &uncorrected);
410 memcpy(buf, bounce_buf + byte, cplen);
425 static int alauda_read(
struct mtd_info *mtd, loff_t from,
size_t len,
426 size_t *retlen,
u_char *buf)
429 int err, corrected=0, uncorrected=0;
432 return alauda_bounce_read(mtd, from, len, retlen, buf);
438 err = alauda_read_page(mtd, from, buf, oob,
439 &corrected, &uncorrected);
455 static int alauda_write(
struct mtd_info *mtd, loff_t to,
size_t len,
456 size_t *retlen,
const u_char *buf)
467 u8 oob[16] = {
'h',
'e',
'l',
'l',
'o', 0xff, 0xff, 0xff,
468 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
472 err = alauda_isbad(mtd, to);
480 err = alauda_write_page(mtd, to, (
void*)buf, oob);
503 err = alauda_isbad(mtd, ofs);
509 err = alauda_erase_block(mtd, ofs);
523 err = __alauda_erase(mtd, instr);
529 static int alauda_init_media(
struct alauda *al)
531 u8 buf[4], *b0=
buf, *b1=buf+1;
541 err = alauda_get_media_status(al, buf);
549 err = alauda_ack_media(al);
555 err = alauda_get_media_status(al, buf);
564 err = alauda_get_media_signatures(al, buf);
568 card = get_card(*b1);
582 mtd->
name =
"alauda";
588 mtd->
_read = alauda_read;
589 mtd->
_write = alauda_write;
590 mtd->
_erase = alauda_erase;
610 static int alauda_check_media(
struct alauda *al)
612 u8 buf[2], *b0 =
buf, *b1 = buf+1;
615 err = alauda_get_media_status(al, buf);
619 if ((*b1 & 0x01) == 0) {
623 if ((*b0 & 0x80) || ((*b0 & 0x1F) == 0x10)) {
629 return alauda_init_media(al);
638 struct usb_host_interface *iface;
647 kref_init(&al->
kref);
648 usb_set_intfdata(interface, al);
653 iface = interface->cur_altsetting;
654 for (i = 0; i < iface->desc.bNumEndpoints; ++
i) {
655 ep = &iface->endpoint[
i].desc;
657 if (usb_endpoint_is_bulk_in(ep)) {
659 }
else if (usb_endpoint_is_bulk_out(ep)) {
667 if (!ep_wr || !ep_in || !ep_out)
671 usb_endpoint_num(ep_wr));
673 usb_endpoint_num(ep_in));
675 usb_endpoint_num(ep_out));
678 memcpy(al+1, al,
sizeof(*al));
686 dev_info(&interface->dev,
"alauda probed\n");
687 alauda_check_media(al);
688 alauda_check_media(al+1);
694 kref_put(&al->
kref, alauda_delete);
698 static void alauda_disconnect(
struct usb_interface *interface)
702 al = usb_get_intfdata(interface);
703 usb_set_intfdata(interface,
NULL);
709 kref_put(&al->
kref, alauda_delete);
711 dev_info(&interface->dev,
"alauda gone");
714 static struct usb_driver alauda_driver = {
716 .probe = alauda_probe,
717 .disconnect = alauda_disconnect,
718 .id_table = alauda_table,