mbcs: Remove lots of global symbols
[linux-2.6/linux-2.6-openrd.git] / drivers / usb / storage / jumpshot.c
blob003fcf5458882d3ec90f07251ea5f7aa2f437d34
1 /* Driver for Lexar "Jumpshot" Compact Flash reader
3 * $Id: jumpshot.c,v 1.7 2002/02/25 00:40:13 mdharm Exp $
5 * jumpshot driver v0.1:
7 * First release
9 * Current development and maintenance by:
10 * (c) 2000 Jimmie Mayfield (mayfield+usb@sackheads.org)
12 * Many thanks to Robert Baruch for the SanDisk SmartMedia reader driver
13 * which I used as a template for this driver.
15 * Some bugfixes and scatter-gather code by Gregory P. Smith
16 * (greg-usb@electricrain.com)
18 * Fix for media change by Joerg Schneider (js@joergschneider.com)
20 * Developed with the assistance of:
22 * (C) 2002 Alan Stern <stern@rowland.org>
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2, or (at your option) any
27 * later version.
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
40 * This driver attempts to support the Lexar Jumpshot USB CompactFlash
41 * reader. Like many other USB CompactFlash readers, the Jumpshot contains
42 * a USB-to-ATA chip.
44 * This driver supports reading and writing. If you're truly paranoid,
45 * however, you can force the driver into a write-protected state by setting
46 * the WP enable bits in jumpshot_handle_mode_sense. See the comments
47 * in that routine.
50 #include <linux/errno.h>
51 #include <linux/slab.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
56 #include "usb.h"
57 #include "transport.h"
58 #include "protocol.h"
59 #include "debug.h"
60 #include "jumpshot.h"
63 static inline int jumpshot_bulk_read(struct us_data *us,
64 unsigned char *data,
65 unsigned int len)
67 if (len == 0)
68 return USB_STOR_XFER_GOOD;
70 US_DEBUGP("jumpshot_bulk_read: len = %d\n", len);
71 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
72 data, len, NULL);
76 static inline int jumpshot_bulk_write(struct us_data *us,
77 unsigned char *data,
78 unsigned int len)
80 if (len == 0)
81 return USB_STOR_XFER_GOOD;
83 US_DEBUGP("jumpshot_bulk_write: len = %d\n", len);
84 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
85 data, len, NULL);
89 static int jumpshot_get_status(struct us_data *us)
91 int rc;
93 if (!us)
94 return USB_STOR_TRANSPORT_ERROR;
96 // send the setup
97 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
98 0, 0xA0, 0, 7, us->iobuf, 1);
100 if (rc != USB_STOR_XFER_GOOD)
101 return USB_STOR_TRANSPORT_ERROR;
103 if (us->iobuf[0] != 0x50) {
104 US_DEBUGP("jumpshot_get_status: 0x%2x\n",
105 us->iobuf[0]);
106 return USB_STOR_TRANSPORT_ERROR;
109 return USB_STOR_TRANSPORT_GOOD;
112 static int jumpshot_read_data(struct us_data *us,
113 struct jumpshot_info *info,
114 u32 sector,
115 u32 sectors)
117 unsigned char *command = us->iobuf;
118 unsigned char *buffer;
119 unsigned char thistime;
120 unsigned int totallen, alloclen;
121 int len, result;
122 unsigned int sg_idx = 0, sg_offset = 0;
124 // we're working in LBA mode. according to the ATA spec,
125 // we can support up to 28-bit addressing. I don't know if Jumpshot
126 // supports beyond 24-bit addressing. It's kind of hard to test
127 // since it requires > 8GB CF card.
129 if (sector > 0x0FFFFFFF)
130 return USB_STOR_TRANSPORT_ERROR;
132 totallen = sectors * info->ssize;
134 // Since we don't read more than 64 KB at a time, we have to create
135 // a bounce buffer and move the data a piece at a time between the
136 // bounce buffer and the actual transfer buffer.
138 alloclen = min(totallen, 65536u);
139 buffer = kmalloc(alloclen, GFP_NOIO);
140 if (buffer == NULL)
141 return USB_STOR_TRANSPORT_ERROR;
143 do {
144 // loop, never allocate or transfer more than 64k at once
145 // (min(128k, 255*info->ssize) is the real limit)
146 len = min(totallen, alloclen);
147 thistime = (len / info->ssize) & 0xff;
149 command[0] = 0;
150 command[1] = thistime;
151 command[2] = sector & 0xFF;
152 command[3] = (sector >> 8) & 0xFF;
153 command[4] = (sector >> 16) & 0xFF;
155 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
156 command[6] = 0x20;
158 // send the setup + command
159 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
160 0, 0x20, 0, 1, command, 7);
161 if (result != USB_STOR_XFER_GOOD)
162 goto leave;
164 // read the result
165 result = jumpshot_bulk_read(us, buffer, len);
166 if (result != USB_STOR_XFER_GOOD)
167 goto leave;
169 US_DEBUGP("jumpshot_read_data: %d bytes\n", len);
171 // Store the data in the transfer buffer
172 usb_stor_access_xfer_buf(buffer, len, us->srb,
173 &sg_idx, &sg_offset, TO_XFER_BUF);
175 sector += thistime;
176 totallen -= len;
177 } while (totallen > 0);
179 kfree(buffer);
180 return USB_STOR_TRANSPORT_GOOD;
182 leave:
183 kfree(buffer);
184 return USB_STOR_TRANSPORT_ERROR;
188 static int jumpshot_write_data(struct us_data *us,
189 struct jumpshot_info *info,
190 u32 sector,
191 u32 sectors)
193 unsigned char *command = us->iobuf;
194 unsigned char *buffer;
195 unsigned char thistime;
196 unsigned int totallen, alloclen;
197 int len, result, waitcount;
198 unsigned int sg_idx = 0, sg_offset = 0;
200 // we're working in LBA mode. according to the ATA spec,
201 // we can support up to 28-bit addressing. I don't know if Jumpshot
202 // supports beyond 24-bit addressing. It's kind of hard to test
203 // since it requires > 8GB CF card.
205 if (sector > 0x0FFFFFFF)
206 return USB_STOR_TRANSPORT_ERROR;
208 totallen = sectors * info->ssize;
210 // Since we don't write more than 64 KB at a time, we have to create
211 // a bounce buffer and move the data a piece at a time between the
212 // bounce buffer and the actual transfer buffer.
214 alloclen = min(totallen, 65536u);
215 buffer = kmalloc(alloclen, GFP_NOIO);
216 if (buffer == NULL)
217 return USB_STOR_TRANSPORT_ERROR;
219 do {
220 // loop, never allocate or transfer more than 64k at once
221 // (min(128k, 255*info->ssize) is the real limit)
223 len = min(totallen, alloclen);
224 thistime = (len / info->ssize) & 0xff;
226 // Get the data from the transfer buffer
227 usb_stor_access_xfer_buf(buffer, len, us->srb,
228 &sg_idx, &sg_offset, FROM_XFER_BUF);
230 command[0] = 0;
231 command[1] = thistime;
232 command[2] = sector & 0xFF;
233 command[3] = (sector >> 8) & 0xFF;
234 command[4] = (sector >> 16) & 0xFF;
236 command[5] = 0xE0 | ((sector >> 24) & 0x0F);
237 command[6] = 0x30;
239 // send the setup + command
240 result = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
241 0, 0x20, 0, 1, command, 7);
242 if (result != USB_STOR_XFER_GOOD)
243 goto leave;
245 // send the data
246 result = jumpshot_bulk_write(us, buffer, len);
247 if (result != USB_STOR_XFER_GOOD)
248 goto leave;
250 // read the result. apparently the bulk write can complete
251 // before the jumpshot drive is finished writing. so we loop
252 // here until we get a good return code
253 waitcount = 0;
254 do {
255 result = jumpshot_get_status(us);
256 if (result != USB_STOR_TRANSPORT_GOOD) {
257 // I have not experimented to find the smallest value.
259 msleep(50);
261 } while ((result != USB_STOR_TRANSPORT_GOOD) && (waitcount < 10));
263 if (result != USB_STOR_TRANSPORT_GOOD)
264 US_DEBUGP("jumpshot_write_data: Gah! Waitcount = 10. Bad write!?\n");
266 sector += thistime;
267 totallen -= len;
268 } while (totallen > 0);
270 kfree(buffer);
271 return result;
273 leave:
274 kfree(buffer);
275 return USB_STOR_TRANSPORT_ERROR;
278 static int jumpshot_id_device(struct us_data *us,
279 struct jumpshot_info *info)
281 unsigned char *command = us->iobuf;
282 unsigned char *reply;
283 int rc;
285 if (!us || !info)
286 return USB_STOR_TRANSPORT_ERROR;
288 command[0] = 0xE0;
289 command[1] = 0xEC;
290 reply = kmalloc(512, GFP_NOIO);
291 if (!reply)
292 return USB_STOR_TRANSPORT_ERROR;
294 // send the setup
295 rc = usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
296 0, 0x20, 0, 6, command, 2);
298 if (rc != USB_STOR_XFER_GOOD) {
299 US_DEBUGP("jumpshot_id_device: Gah! "
300 "send_control for read_capacity failed\n");
301 rc = USB_STOR_TRANSPORT_ERROR;
302 goto leave;
305 // read the reply
306 rc = jumpshot_bulk_read(us, reply, 512);
307 if (rc != USB_STOR_XFER_GOOD) {
308 rc = USB_STOR_TRANSPORT_ERROR;
309 goto leave;
312 info->sectors = ((u32)(reply[117]) << 24) |
313 ((u32)(reply[116]) << 16) |
314 ((u32)(reply[115]) << 8) |
315 ((u32)(reply[114]) );
317 rc = USB_STOR_TRANSPORT_GOOD;
319 leave:
320 kfree(reply);
321 return rc;
324 static int jumpshot_handle_mode_sense(struct us_data *us,
325 struct scsi_cmnd * srb,
326 int sense_6)
328 static unsigned char rw_err_page[12] = {
329 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
331 static unsigned char cache_page[12] = {
332 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
334 static unsigned char rbac_page[12] = {
335 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
337 static unsigned char timer_page[8] = {
338 0x1C, 0x6, 0, 0, 0, 0
340 unsigned char pc, page_code;
341 unsigned int i = 0;
342 struct jumpshot_info *info = (struct jumpshot_info *) (us->extra);
343 unsigned char *ptr = us->iobuf;
345 pc = srb->cmnd[2] >> 6;
346 page_code = srb->cmnd[2] & 0x3F;
348 switch (pc) {
349 case 0x0:
350 US_DEBUGP("jumpshot_handle_mode_sense: Current values\n");
351 break;
352 case 0x1:
353 US_DEBUGP("jumpshot_handle_mode_sense: Changeable values\n");
354 break;
355 case 0x2:
356 US_DEBUGP("jumpshot_handle_mode_sense: Default values\n");
357 break;
358 case 0x3:
359 US_DEBUGP("jumpshot_handle_mode_sense: Saves values\n");
360 break;
363 memset(ptr, 0, 8);
364 if (sense_6) {
365 ptr[2] = 0x00; // WP enable: 0x80
366 i = 4;
367 } else {
368 ptr[3] = 0x00; // WP enable: 0x80
369 i = 8;
372 switch (page_code) {
373 case 0x0:
374 // vendor-specific mode
375 info->sense_key = 0x05;
376 info->sense_asc = 0x24;
377 info->sense_ascq = 0x00;
378 return USB_STOR_TRANSPORT_FAILED;
380 case 0x1:
381 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
382 i += sizeof(rw_err_page);
383 break;
385 case 0x8:
386 memcpy(ptr + i, cache_page, sizeof(cache_page));
387 i += sizeof(cache_page);
388 break;
390 case 0x1B:
391 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
392 i += sizeof(rbac_page);
393 break;
395 case 0x1C:
396 memcpy(ptr + i, timer_page, sizeof(timer_page));
397 i += sizeof(timer_page);
398 break;
400 case 0x3F:
401 memcpy(ptr + i, timer_page, sizeof(timer_page));
402 i += sizeof(timer_page);
403 memcpy(ptr + i, rbac_page, sizeof(rbac_page));
404 i += sizeof(rbac_page);
405 memcpy(ptr + i, cache_page, sizeof(cache_page));
406 i += sizeof(cache_page);
407 memcpy(ptr + i, rw_err_page, sizeof(rw_err_page));
408 i += sizeof(rw_err_page);
409 break;
412 if (sense_6)
413 ptr[0] = i - 1;
414 else
415 ((__be16 *) ptr)[0] = cpu_to_be16(i - 2);
416 usb_stor_set_xfer_buf(ptr, i, srb);
418 return USB_STOR_TRANSPORT_GOOD;
422 static void jumpshot_info_destructor(void *extra)
424 // this routine is a placeholder...
425 // currently, we don't allocate any extra blocks so we're okay
430 // Transport for the Lexar 'Jumpshot'
432 int jumpshot_transport(struct scsi_cmnd * srb, struct us_data *us)
434 struct jumpshot_info *info;
435 int rc;
436 unsigned long block, blocks;
437 unsigned char *ptr = us->iobuf;
438 static unsigned char inquiry_response[8] = {
439 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
442 if (!us->extra) {
443 us->extra = kzalloc(sizeof(struct jumpshot_info), GFP_NOIO);
444 if (!us->extra) {
445 US_DEBUGP("jumpshot_transport: Gah! Can't allocate storage for jumpshot info struct!\n");
446 return USB_STOR_TRANSPORT_ERROR;
448 us->extra_destructor = jumpshot_info_destructor;
451 info = (struct jumpshot_info *) (us->extra);
453 if (srb->cmnd[0] == INQUIRY) {
454 US_DEBUGP("jumpshot_transport: INQUIRY. Returning bogus response.\n");
455 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
456 fill_inquiry_response(us, ptr, 36);
457 return USB_STOR_TRANSPORT_GOOD;
460 if (srb->cmnd[0] == READ_CAPACITY) {
461 info->ssize = 0x200; // hard coded 512 byte sectors as per ATA spec
463 rc = jumpshot_get_status(us);
464 if (rc != USB_STOR_TRANSPORT_GOOD)
465 return rc;
467 rc = jumpshot_id_device(us, info);
468 if (rc != USB_STOR_TRANSPORT_GOOD)
469 return rc;
471 US_DEBUGP("jumpshot_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
472 info->sectors, info->ssize);
474 // build the reply
476 ((__be32 *) ptr)[0] = cpu_to_be32(info->sectors - 1);
477 ((__be32 *) ptr)[1] = cpu_to_be32(info->ssize);
478 usb_stor_set_xfer_buf(ptr, 8, srb);
480 return USB_STOR_TRANSPORT_GOOD;
483 if (srb->cmnd[0] == MODE_SELECT_10) {
484 US_DEBUGP("jumpshot_transport: Gah! MODE_SELECT_10.\n");
485 return USB_STOR_TRANSPORT_ERROR;
488 if (srb->cmnd[0] == READ_10) {
489 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
490 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
492 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
494 US_DEBUGP("jumpshot_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
495 return jumpshot_read_data(us, info, block, blocks);
498 if (srb->cmnd[0] == READ_12) {
499 // I don't think we'll ever see a READ_12 but support it anyway...
501 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
502 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
504 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
505 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
507 US_DEBUGP("jumpshot_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
508 return jumpshot_read_data(us, info, block, blocks);
511 if (srb->cmnd[0] == WRITE_10) {
512 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
513 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
515 blocks = ((u32)(srb->cmnd[7]) << 8) | ((u32)(srb->cmnd[8]));
517 US_DEBUGP("jumpshot_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
518 return jumpshot_write_data(us, info, block, blocks);
521 if (srb->cmnd[0] == WRITE_12) {
522 // I don't think we'll ever see a WRITE_12 but support it anyway...
524 block = ((u32)(srb->cmnd[2]) << 24) | ((u32)(srb->cmnd[3]) << 16) |
525 ((u32)(srb->cmnd[4]) << 8) | ((u32)(srb->cmnd[5]));
527 blocks = ((u32)(srb->cmnd[6]) << 24) | ((u32)(srb->cmnd[7]) << 16) |
528 ((u32)(srb->cmnd[8]) << 8) | ((u32)(srb->cmnd[9]));
530 US_DEBUGP("jumpshot_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
531 return jumpshot_write_data(us, info, block, blocks);
535 if (srb->cmnd[0] == TEST_UNIT_READY) {
536 US_DEBUGP("jumpshot_transport: TEST_UNIT_READY.\n");
537 return jumpshot_get_status(us);
540 if (srb->cmnd[0] == REQUEST_SENSE) {
541 US_DEBUGP("jumpshot_transport: REQUEST_SENSE.\n");
543 memset(ptr, 0, 18);
544 ptr[0] = 0xF0;
545 ptr[2] = info->sense_key;
546 ptr[7] = 11;
547 ptr[12] = info->sense_asc;
548 ptr[13] = info->sense_ascq;
549 usb_stor_set_xfer_buf(ptr, 18, srb);
551 return USB_STOR_TRANSPORT_GOOD;
554 if (srb->cmnd[0] == MODE_SENSE) {
555 US_DEBUGP("jumpshot_transport: MODE_SENSE_6 detected\n");
556 return jumpshot_handle_mode_sense(us, srb, 1);
559 if (srb->cmnd[0] == MODE_SENSE_10) {
560 US_DEBUGP("jumpshot_transport: MODE_SENSE_10 detected\n");
561 return jumpshot_handle_mode_sense(us, srb, 0);
564 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
565 // sure. whatever. not like we can stop the user from popping
566 // the media out of the device (no locking doors, etc)
568 return USB_STOR_TRANSPORT_GOOD;
571 if (srb->cmnd[0] == START_STOP) {
572 /* this is used by sd.c'check_scsidisk_media_change to detect
573 media change */
574 US_DEBUGP("jumpshot_transport: START_STOP.\n");
575 /* the first jumpshot_id_device after a media change returns
576 an error (determined experimentally) */
577 rc = jumpshot_id_device(us, info);
578 if (rc == USB_STOR_TRANSPORT_GOOD) {
579 info->sense_key = NO_SENSE;
580 srb->result = SUCCESS;
581 } else {
582 info->sense_key = UNIT_ATTENTION;
583 srb->result = SAM_STAT_CHECK_CONDITION;
585 return rc;
588 US_DEBUGP("jumpshot_transport: Gah! Unknown command: %d (0x%x)\n",
589 srb->cmnd[0], srb->cmnd[0]);
590 info->sense_key = 0x05;
591 info->sense_asc = 0x20;
592 info->sense_ascq = 0x00;
593 return USB_STOR_TRANSPORT_FAILED;