microtek.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Driver for Microtek Scanmaker X6 USB scanner, and possibly others.
  2.  *
  3.  * (C) Copyright 2000 John Fremlin <vii@penguinpowered.com>
  4.  * (C) Copyright 2000 Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>
  5.  *
  6.  * Parts shamelessly stolen from usb-storage and copyright by their
  7.  * authors. Thanks to Matt Dharm for giving us permission!
  8.  *
  9.  * This driver implements a SCSI host controller driver and a USB
  10.  * device driver. To avoid confusion, all the USB related stuff is
  11.  * prefixed by mts_usb_ and all the SCSI stuff by mts_scsi_.
  12.  *
  13.  * Microtek (www.microtek.com) did not release the specifications for
  14.  * their USB protocol to us, so we had to reverse engineer them. We
  15.  * don't know for which models they are valid.
  16.  *
  17.  * The X6 USB has three bulk endpoints, one output (0x1) down which
  18.  * commands and outgoing data are sent, and two input: 0x82 from which
  19.  * normal data is read from the scanner (in packets of maximum 32
  20.  * bytes) and from which the status byte is read, and 0x83 from which
  21.  * the results of a scan (or preview) are read in up to 64 * 1024 byte
  22.  * chunks by the Windows driver. We don't know how much it is possible
  23.  * to read at a time from 0x83.
  24.  *
  25.  * It seems possible to read (with URB transfers) everything from 0x82
  26.  * in one go, without bothering to read in 32 byte chunks.
  27.  *
  28.  * There seems to be an optimisation of a further READ implicit if
  29.  * you simply read from 0x83.
  30.  *
  31.  * Guessed protocol:
  32.  *
  33.  * Send raw SCSI command to EP 0x1
  34.  *
  35.  * If there is data to receive:
  36.  * If the command was READ datatype=image:
  37.  * Read a lot of data from EP 0x83
  38.  * Else:
  39.  * Read data from EP 0x82
  40.  * Else:
  41.  * If there is data to transmit:
  42.  * Write it to EP 0x1
  43.  *
  44.  * Read status byte from EP 0x82
  45.  *
  46.  * References:
  47.  *
  48.  * The SCSI command set for the scanner is available from
  49.  * ftp://ftp.microtek.com/microtek/devpack/
  50.  *
  51.  * Microtek NV sent us a more up to date version of the document. If
  52.  * you want it, just send mail.
  53.  *
  54.  * Status:
  55.  *
  56.  * Untested with multiple scanners.
  57.  * Untested on SMP.
  58.  * Untested on a bigendian machine.
  59.  *
  60.  * History:
  61.  *
  62.  * 20000417 starting history
  63.  * 20000417 fixed load oops
  64.  * 20000417 fixed unload oops
  65.  * 20000419 fixed READ IMAGE detection
  66.  * 20000424 started conversion to use URBs
  67.  * 20000502 handled short transfers as errors
  68.  * 20000513 rename and organisation of functions (john)
  69.  * 20000513 added IDs for all products supported by Windows driver (john)
  70.  * 20000514 Rewrote mts_scsi_queuecommand to use URBs (john)
  71.  * 20000514 Version 0.0.8j
  72.  *      20000514 Fix reporting of non-existant devices to SCSI layer (john)
  73.  * 20000514 Added MTS_DEBUG_INT (john)
  74.  * 20000514 Changed "usb-microtek" to "microtek" for consistency (john)
  75.  * 20000514 Stupid bug fixes (john)
  76.  * 20000514 Version 0.0.9j
  77.  * 20000515 Put transfer context and URB in mts_desc (john)
  78.  * 20000515 Added prelim turn off debugging support (john)
  79.  * 20000515 Version 0.0.10j
  80.  *      20000515 Fixed up URB allocation (clear URB on alloc) (john)
  81.  *      20000515 Version 0.0.11j
  82.  * 20000516 Removed unnecessary spinlock in mts_transfer_context (john)
  83.  * 20000516 Removed unnecessary up on instance lock in mts_remove_nolock (john)
  84.  * 20000516 Implemented (badly) scsi_abort (john)
  85.  * 20000516 Version 0.0.12j
  86.  *      20000517 Hopefully removed mts_remove_nolock quasideadlock (john)
  87.  *      20000517 Added mts_debug_dump to print ll USB info (john)
  88.  * 20000518 Tweaks and documentation updates (john)
  89.  * 20000518 Version 0.0.13j
  90.  * 20000518 Cleaned up abort handling (john)
  91.  * 20000523 Removed scsi_command and various scsi_..._resets (john)
  92.  * 20000523 Added unlink URB on scsi_abort, now OHCI supports it (john)
  93.  * 20000523 Fixed last tiresome compile warning (john)
  94.  * 20000523 Version 0.0.14j (though version 0.1 has come out?)
  95.  * 20000602 Added primitive reset
  96.  * 20000602 Version 0.2.0
  97.  * 20000603 various cosmetic changes
  98.  * 20000603 Version 0.2.1
  99.  * 20000620 minor cosmetic changes
  100.  * 20000620 Version 0.2.2
  101.  * 20000822 Hopefully fixed deadlock in mts_remove_nolock()
  102.  * 20000822 Fixed minor race in mts_transfer_cleanup()
  103.  * 20000822 Fixed deadlock on submission error in queuecommand
  104.  * 20000822 Version 0.2.3
  105.  * 20000913 Reduced module size if debugging is off
  106.  * 20000913 Version 0.2.4
  107.  *      20010210 New abort logic
  108.  *      20010210 Version 0.3.0
  109.  * 20010217 Merged scatter/gather
  110.  * 20010218 Version 0.4.0
  111.  * 20010218 Cosmetic fixes
  112.  * 20010218 Version 0.4.1
  113.  *      20010306 Abort while using scatter/gather
  114.  *      20010306 Version 0.4.2
  115.  *      20010311 Remove all timeouts and tidy up generally (john)
  116.  * 20010320 check return value of scsi_register()
  117.  * 20010320 Version 0.4.3
  118.  * 20010408 Identify version on module load.
  119.  * 20011003 Fix multiple requests
  120.  * 20020618 Version 0.4.4
  121.  * 20020618 Confirm to utterly stupid rules about io_request_lock
  122.  */
  123. #include <linux/module.h>
  124. #include <linux/kernel.h>
  125. #include <linux/sched.h>
  126. #include <linux/signal.h>
  127. #include <linux/errno.h>
  128. #include <linux/random.h>
  129. #include <linux/poll.h>
  130. #include <linux/init.h>
  131. #include <linux/slab.h>
  132. #include <linux/spinlock.h>
  133. #include <linux/smp_lock.h>
  134. #include <linux/usb.h>
  135. #include <linux/proc_fs.h>
  136. #include <asm/atomic.h>
  137. #include <linux/blk.h>
  138. #include "../scsi/scsi.h"
  139. #include "../scsi/hosts.h"
  140. #include "../scsi/sd.h"
  141. #include "microtek.h"
  142. /*
  143.  * Version Information
  144.  */
  145. #define DRIVER_VERSION "v0.4.4"
  146. #define DRIVER_AUTHOR "John Fremlin <vii@penguinpowered.com>, Oliver Neukum <Oliver.Neukum@lrz.uni-muenchen.de>"
  147. #define DRIVER_DESC "Microtek Scanmaker X6 USB scanner driver"
  148. /* Should we do debugging? */
  149. //#define MTS_DO_DEBUG
  150. /* USB layer driver interface */
  151. static void *mts_usb_probe(struct usb_device *dev, unsigned int interface,
  152.  const struct usb_device_id *id);
  153. static void mts_usb_disconnect(struct usb_device *dev, void *ptr);
  154. static struct usb_device_id mts_usb_ids [];
  155. static struct usb_driver mts_usb_driver = {
  156. name: "microtekX6",
  157. probe: mts_usb_probe,
  158. disconnect: mts_usb_disconnect,
  159. id_table: mts_usb_ids,
  160. };
  161. /* Internal driver stuff */
  162. #define MTS_VERSION "0.4.3"
  163. #define MTS_NAME "microtek usb (rev " MTS_VERSION "): "
  164. #define MTS_WARNING(x...) 
  165. printk( KERN_WARNING MTS_NAME x )
  166. #define MTS_ERROR(x...) 
  167. printk( KERN_ERR MTS_NAME x )
  168. #define MTS_INT_ERROR(x...) 
  169. MTS_ERROR(x)
  170. #define MTS_MESSAGE(x...) 
  171. printk( KERN_INFO MTS_NAME x )
  172. #if defined MTS_DO_DEBUG
  173. #define MTS_DEBUG(x...) 
  174. printk( KERN_DEBUG MTS_NAME x )
  175. #define MTS_DEBUG_GOT_HERE() 
  176. MTS_DEBUG("got to %s:%d (%s)n", __FILE__, (int)__LINE__, __PRETTY_FUNCTION__ )
  177. #define MTS_DEBUG_INT() 
  178. do { MTS_DEBUG_GOT_HERE(); 
  179.      MTS_DEBUG("transfer = 0x%x context = 0x%xn",(int)transfer,(int)context ); 
  180.      MTS_DEBUG("status = 0x%x data-length = 0x%x sent = 0x%xn",(int)transfer->status,(int)context->data_length, (int)transfer->actual_length ); 
  181.              mts_debug_dump(context->instance);
  182.    } while(0)
  183. #else
  184. #define MTS_NUL_STATEMENT do { } while(0)
  185. #define MTS_DEBUG(x...) MTS_NUL_STATEMENT
  186. #define MTS_DEBUG_GOT_HERE() MTS_NUL_STATEMENT
  187. #define MTS_DEBUG_INT() MTS_NUL_STATEMENT
  188. #endif
  189. #define MTS_INT_INIT()
  190. struct mts_transfer_context* context = (struct mts_transfer_context*)transfer->context; 
  191. MTS_DEBUG_INT();
  192. #ifdef MTS_DO_DEBUG
  193. static inline void mts_debug_dump(struct mts_desc* desc) {
  194. MTS_DEBUG("desc at 0x%x: halted = %02x%02x, toggle = %02x%02xn",
  195.   (int)desc,(int)desc->usb_dev->halted[1],(int)desc->usb_dev->halted[0],
  196.   (int)desc->usb_dev->toggle[1],(int)desc->usb_dev->toggle[0]
  197. );
  198. MTS_DEBUG("ep_out=%x ep_response=%x ep_image=%xn",
  199.   usb_sndbulkpipe(desc->usb_dev,desc->ep_out),
  200.   usb_rcvbulkpipe(desc->usb_dev,desc->ep_response),
  201.   usb_rcvbulkpipe(desc->usb_dev,desc->ep_image)
  202. );
  203. }
  204. static inline void mts_show_command(Scsi_Cmnd *srb)
  205. {
  206. char *what = NULL;
  207. switch (srb->cmnd[0]) {
  208. case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;
  209. case REZERO_UNIT: what = "REZERO_UNIT"; break;
  210. case REQUEST_SENSE: what = "REQUEST_SENSE"; break;
  211. case FORMAT_UNIT: what = "FORMAT_UNIT"; break;
  212. case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;
  213. case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;
  214. case READ_6: what = "READ_6"; break;
  215. case WRITE_6: what = "WRITE_6"; break;
  216. case SEEK_6: what = "SEEK_6"; break;
  217. case READ_REVERSE: what = "READ_REVERSE"; break;
  218. case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;
  219. case SPACE: what = "SPACE"; break;
  220. case INQUIRY: what = "INQUIRY"; break;
  221. case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;
  222. case MODE_SELECT: what = "MODE_SELECT"; break;
  223. case RESERVE: what = "RESERVE"; break;
  224. case RELEASE: what = "RELEASE"; break;
  225. case COPY: what = "COPY"; break;
  226. case ERASE: what = "ERASE"; break;
  227. case MODE_SENSE: what = "MODE_SENSE"; break;
  228. case START_STOP: what = "START_STOP"; break;
  229. case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;
  230. case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;
  231. case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;
  232. case SET_WINDOW: what = "SET_WINDOW"; break;
  233. case READ_CAPACITY: what = "READ_CAPACITY"; break;
  234. case READ_10: what = "READ_10"; break;
  235. case WRITE_10: what = "WRITE_10"; break;
  236. case SEEK_10: what = "SEEK_10"; break;
  237. case WRITE_VERIFY: what = "WRITE_VERIFY"; break;
  238. case VERIFY: what = "VERIFY"; break;
  239. case SEARCH_HIGH: what = "SEARCH_HIGH"; break;
  240. case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;
  241. case SEARCH_LOW: what = "SEARCH_LOW"; break;
  242. case SET_LIMITS: what = "SET_LIMITS"; break;
  243. case READ_POSITION: what = "READ_POSITION"; break;
  244. case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;
  245. case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;
  246. case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;
  247. case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;
  248. case COMPARE: what = "COMPARE"; break;
  249. case COPY_VERIFY: what = "COPY_VERIFY"; break;
  250. case WRITE_BUFFER: what = "WRITE_BUFFER"; break;
  251. case READ_BUFFER: what = "READ_BUFFER"; break;
  252. case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;
  253. case READ_LONG: what = "READ_LONG"; break;
  254. case WRITE_LONG: what = "WRITE_LONG"; break;
  255. case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;
  256. case WRITE_SAME: what = "WRITE_SAME"; break;
  257. case READ_TOC: what = "READ_TOC"; break;
  258. case LOG_SELECT: what = "LOG_SELECT"; break;
  259. case LOG_SENSE: what = "LOG_SENSE"; break;
  260. case MODE_SELECT_10: what = "MODE_SELECT_10"; break;
  261. case MODE_SENSE_10: what = "MODE_SENSE_10"; break;
  262. case MOVE_MEDIUM: what = "MOVE_MEDIUM"; break;
  263. case READ_12: what = "READ_12"; break;
  264. case WRITE_12: what = "WRITE_12"; break;
  265. case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;
  266. case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;
  267. case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;
  268. case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;
  269. case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;
  270. case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;
  271. case WRITE_LONG_2: what = "WRITE_LONG_2"; break;
  272. default:
  273. MTS_DEBUG("can't decode commandn");
  274. goto out;
  275. break;
  276. }
  277. MTS_DEBUG( "Command %s (%d bytes)n", what, srb->cmd_len);
  278.  out:
  279. MTS_DEBUG( "  %02x %02x %02x %02x %02x %02x %02x %02x %02x %02xn",
  280.        srb->cmnd[0], srb->cmnd[1], srb->cmnd[2], srb->cmnd[3], srb->cmnd[4], srb->cmnd[5],
  281.        srb->cmnd[6], srb->cmnd[7], srb->cmnd[8], srb->cmnd[9]);
  282. }
  283. #else
  284. static inline void mts_show_command(Scsi_Cmnd * dummy)
  285. {
  286. }
  287. static inline void mts_debug_dump(struct mts_desc* dummy)
  288. {
  289. }
  290. #endif
  291. /* static inline int mts_is_aborting(struct mts_desc* desc) {
  292. return (atomic_read(&desc->context.do_abort));
  293. }  */
  294. static inline void mts_urb_abort(struct mts_desc* desc) {
  295. spin_unlock_irq(&io_request_lock);
  296. MTS_DEBUG_GOT_HERE();
  297. mts_debug_dump(desc);
  298. usb_unlink_urb( &desc->urb );
  299. spin_lock_irq(&io_request_lock);
  300. }
  301. static struct mts_desc * mts_list; /* list of active scanners */
  302. struct semaphore mts_list_semaphore;
  303. /* Internal list operations */
  304. static
  305. void mts_remove_nolock( struct mts_desc* to_remove )
  306. {
  307. MTS_DEBUG( "removing 0x%x from listn",
  308.    (int)to_remove );
  309. lock_kernel();
  310. mts_urb_abort(to_remove);
  311. MTS_DEBUG_GOT_HERE();
  312. if ( to_remove != mts_list ) {
  313. MTS_DEBUG_GOT_HERE();
  314. if (to_remove->prev && to_remove->next)
  315. to_remove->prev->next = to_remove->next;
  316. } else {
  317. MTS_DEBUG_GOT_HERE();
  318. mts_list = to_remove->next;
  319. if (mts_list) {
  320. MTS_DEBUG_GOT_HERE();
  321. mts_list->prev = 0;
  322. }
  323. }
  324. if ( to_remove->next ) {
  325. MTS_DEBUG_GOT_HERE();
  326. to_remove->next->prev = to_remove->prev;
  327. }
  328. MTS_DEBUG_GOT_HERE();
  329. scsi_unregister_module(MODULE_SCSI_HA, &(to_remove->ctempl));
  330. unlock_kernel();
  331. kfree( to_remove );
  332. }
  333. static
  334. void mts_add_nolock( struct mts_desc* to_add )
  335. {
  336. MTS_DEBUG( "adding 0x%x to listn", (int)to_add );
  337. to_add->prev = 0;
  338. to_add->next = mts_list;
  339. if ( mts_list ) {
  340. mts_list->prev = to_add;
  341. }
  342. mts_list = to_add;
  343. }
  344. /* SCSI driver interface */
  345. /* scsi related functions - dummies for now mostly */
  346. static int mts_scsi_release(struct Scsi_Host *psh)
  347. {
  348. MTS_DEBUG_GOT_HERE();
  349. return 0;
  350. }
  351. static int mts_scsi_abort (Scsi_Cmnd *srb)
  352. {
  353. struct mts_desc* desc = (struct mts_desc*)(srb->host->hostdata[0]);
  354. MTS_DEBUG_GOT_HERE();
  355. mts_urb_abort(desc);
  356. return SCSI_ABORT_PENDING;
  357. }
  358. static int mts_scsi_host_reset (Scsi_Cmnd *srb)
  359. {
  360. struct mts_desc* desc = (struct mts_desc*)(srb->host->hostdata[0]);
  361. spin_unlock_irq(&io_request_lock);
  362. MTS_DEBUG_GOT_HERE();
  363. mts_debug_dump(desc);
  364. usb_reset_device(desc->usb_dev); /*FIXME: untested on new reset code */
  365. spin_lock_irq(&io_request_lock);
  366. return 0;  /* RANT why here 0 and not SUCCESS */
  367. }
  368. /* the core of the scsi part */
  369. /* faking a detection - which can't fail :-) */
  370. static int mts_scsi_detect (struct SHT * sht)
  371. {
  372. /* Whole function stolen from usb-storage */
  373. struct mts_desc * desc = (struct mts_desc *)sht->proc_dir;
  374. /* What a hideous hack! */
  375. char local_name[48];
  376. spin_unlock_irq(&io_request_lock);
  377. MTS_DEBUG_GOT_HERE();
  378. /* set up the name of our subdirectory under /proc/scsi/ */
  379. sprintf(local_name, "microtek-%d", desc->host_number);
  380. sht->proc_name = kmalloc (strlen(local_name) + 1, GFP_KERNEL);
  381. /* FIXME: where is this freed ? */
  382. if (!sht->proc_name) {
  383. MTS_ERROR( "unable to allocate memory for proc interface!!n" );
  384. spin_lock_irq(&io_request_lock);
  385. return 0;
  386. }
  387. strcpy(sht->proc_name, local_name);
  388.   sht->proc_dir = NULL;
  389. /* In host->hostdata we store a pointer to desc */
  390. desc->host = scsi_register(sht, sizeof(desc));
  391. if (desc->host == NULL) {
  392. MTS_ERROR("Cannot register due to low memory");
  393. kfree(sht->proc_name);
  394. spin_lock_irq(&io_request_lock);
  395. return 0;
  396. }
  397. desc->host->hostdata[0] = (unsigned long)desc;
  398. /* FIXME: what if sizeof(void*) != sizeof(unsigned long)? */
  399. spin_lock_irq(&io_request_lock);
  400. return 1;
  401. }
  402. /* Main entrypoint: SCSI commands are dispatched to here */
  403. static
  404. int mts_scsi_queuecommand (Scsi_Cmnd *srb, mts_scsi_cmnd_callback callback );
  405. static void mts_transfer_cleanup( struct urb *transfer );
  406. static void mts_do_sg(struct urb * transfer);
  407. inline static
  408. void mts_int_submit_urb (struct urb* transfer,
  409. int pipe,
  410. void* data,
  411. unsigned length,
  412. mts_usb_urb_callback callback )
  413. /* Interrupt context! */
  414. /* Holding transfer->context->lock! */
  415. {
  416. int res;
  417. MTS_INT_INIT();
  418. FILL_BULK_URB(transfer,
  419.       context->instance->usb_dev,
  420.       pipe,
  421.       data,
  422.       length,
  423.       callback,
  424.       context
  425. );
  426. transfer->status = 0;
  427. res = usb_submit_urb( transfer );
  428. if ( res ) {
  429. MTS_INT_ERROR( "could not submit URB! Error was %dn",(int)res );
  430. context->srb->result = DID_ERROR << 16;
  431. mts_transfer_cleanup(transfer);
  432. }
  433. return;
  434. }
  435. static void mts_transfer_cleanup( struct urb *transfer )
  436. /* Interrupt context! */
  437. {
  438. MTS_INT_INIT();
  439. if ( context->final_callback )
  440. context->final_callback(context->srb);
  441. }
  442. static void mts_transfer_done( struct urb *transfer )
  443. {
  444. MTS_INT_INIT();
  445. context->srb->result &= MTS_SCSI_ERR_MASK;
  446. context->srb->result |= (unsigned)context->status<<1;
  447. mts_transfer_cleanup(transfer);
  448. return;
  449. }
  450. static void mts_get_status( struct urb *transfer )
  451. /* Interrupt context! */
  452. {
  453. MTS_INT_INIT();
  454. mts_int_submit_urb(transfer,
  455.    usb_rcvbulkpipe(context->instance->usb_dev,
  456.    context->instance->ep_response),
  457.    &context->status,
  458.    1,
  459.    mts_transfer_done );
  460. }
  461. static void mts_data_done( struct urb* transfer )
  462. /* Interrupt context! */
  463. {
  464. MTS_INT_INIT();
  465. if ( context->data_length != transfer->actual_length ) {
  466. context->srb->resid = context->data_length - transfer->actual_length;
  467. } else if ( transfer->status ) {
  468. context->srb->result = (transfer->status == -ENOENT ? DID_ABORT : DID_ERROR)<<16;
  469. }
  470. mts_get_status(transfer);
  471. return;
  472. }
  473. static void mts_command_done( struct urb *transfer )
  474. /* Interrupt context! */
  475. {
  476. MTS_INT_INIT();
  477. if ( transfer->status ) {
  478.         if (transfer->status == -ENOENT) {
  479.         /* We are being killed */
  480. MTS_DEBUG_GOT_HERE();
  481. context->srb->result = DID_ABORT<<16;
  482.                 } else {
  483.         /* A genuine error has occured */
  484. MTS_DEBUG_GOT_HERE();
  485.         context->srb->result = DID_ERROR<<16;
  486.                 }
  487. mts_transfer_cleanup(transfer);
  488. return;
  489. }
  490. if ( context->data ) {
  491. mts_int_submit_urb(transfer,
  492.    context->data_pipe,
  493.    context->data,
  494.    context->data_length,
  495.    context->srb->use_sg ? mts_do_sg : mts_data_done);
  496. } else mts_get_status(transfer);
  497. return;
  498. }
  499. static void mts_do_sg (struct urb* transfer)
  500. {
  501. struct scatterlist * sg;
  502. MTS_INT_INIT();
  503. MTS_DEBUG("Processing fragment %d of %dn", context->fragment,context->srb->use_sg);
  504. if (transfer->status) {
  505.                 context->srb->result = (transfer->status == -ENOENT ? DID_ABORT : DID_ERROR)<<16;
  506. mts_transfer_cleanup(transfer);
  507.         }
  508. sg = context->srb->buffer;
  509. context->fragment++;
  510. mts_int_submit_urb(transfer,
  511. context->data_pipe,
  512. sg[context->fragment].address,
  513. sg[context->fragment].length,
  514. context->fragment + 1 == context->srb->use_sg ? mts_data_done : mts_do_sg);
  515. return;
  516. }
  517. static const u8 mts_read_image_sig[] = { 0x28, 00, 00, 00 };
  518. static const u8 mts_read_image_sig_len = 4;
  519. static const unsigned char mts_direction[256/8] = {
  520. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  521. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  522. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  523. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  524. };
  525. #define MTS_DIRECTION_IS_IN(x) ((mts_direction[x>>3] >> (x & 7)) & 1)
  526. static void
  527. mts_build_transfer_context( Scsi_Cmnd *srb, struct mts_desc* desc )
  528. {
  529. int pipe;
  530. struct scatterlist * sg;
  531. MTS_DEBUG_GOT_HERE();
  532. desc->context.instance = desc;
  533. desc->context.srb = srb;
  534. desc->context.fragment = 0;
  535. if (!srb->use_sg) {
  536. if ( !srb->bufflen ){
  537. desc->context.data = 0;
  538. desc->context.data_length = 0;
  539. return;
  540. } else {
  541. desc->context.data = srb->buffer;
  542. desc->context.data_length = srb->bufflen;
  543. MTS_DEBUG("length = %d or %dn",
  544.   srb->request_bufflen, srb->bufflen);
  545. }
  546. } else {
  547. MTS_DEBUG("Using scatter/gathern");
  548. sg = srb->buffer;
  549. desc->context.data = sg[0].address;
  550. desc->context.data_length = sg[0].length;
  551. }
  552. /* can't rely on srb->sc_data_direction */
  553. /* Brutally ripped from usb-storage */
  554. if ( !memcmp( srb->cmnd, mts_read_image_sig, mts_read_image_sig_len )
  555. ) {  pipe = usb_rcvbulkpipe(desc->usb_dev,desc->ep_image);
  556. MTS_DEBUG( "transfering from desc->ep_image == %dn",
  557.    (int)desc->ep_image );
  558. } else if ( MTS_DIRECTION_IS_IN(srb->cmnd[0]) ) {
  559. pipe = usb_rcvbulkpipe(desc->usb_dev,desc->ep_response);
  560. MTS_DEBUG( "transfering from desc->ep_response == %dn",
  561.    (int)desc->ep_response);
  562. } else {
  563. MTS_DEBUG("transfering to desc->ep_out == %dn",
  564.   (int)desc->ep_out);
  565. pipe = usb_sndbulkpipe(desc->usb_dev,desc->ep_out);
  566. }
  567. desc->context.data_pipe = pipe;
  568. }
  569. static
  570. int mts_scsi_queuecommand( Scsi_Cmnd *srb, mts_scsi_cmnd_callback callback )
  571. {
  572. struct mts_desc* desc = (struct mts_desc*)(srb->host->hostdata[0]);
  573. int err = 0;
  574. int res;
  575. MTS_DEBUG_GOT_HERE();
  576. mts_show_command(srb);
  577. mts_debug_dump(desc);
  578. if ( srb->device->lun || srb->device->id || srb->device->channel ) {
  579. MTS_DEBUG("Command to LUN=%d ID=%d CHANNEL=%d from SCSI layern",(int)srb->device->lun,(int)srb->device->id, (int)srb->device->channel );
  580. MTS_DEBUG("this device doesn't existn");
  581. srb->result = DID_BAD_TARGET << 16;
  582. if(callback)
  583. callback(srb);
  584. goto out;
  585. }
  586. FILL_BULK_URB(&desc->urb,
  587.       desc->usb_dev,
  588.       usb_sndbulkpipe(desc->usb_dev,desc->ep_out),
  589.       srb->cmnd,
  590.       srb->cmd_len,
  591.       mts_command_done,
  592.       &desc->context
  593.       );
  594. mts_build_transfer_context( srb, desc );
  595. desc->context.final_callback = callback;
  596. res=usb_submit_urb(&desc->urb);
  597. if(res){
  598. MTS_ERROR("error %d submitting URBn",(int)res);
  599. srb->result = DID_ERROR << 16;
  600. if(callback)
  601. callback(srb);
  602. }
  603. out:
  604. return err;
  605. }
  606. /*
  607.  * this defines our 'host'
  608.  */
  609. /* NOTE: This is taken from usb-storage, should be right. */
  610. static Scsi_Host_Template mts_scsi_host_template = {
  611. name:           "microtekX6",
  612. detect: mts_scsi_detect,
  613. release: mts_scsi_release,
  614. queuecommand: mts_scsi_queuecommand,
  615. eh_abort_handler: mts_scsi_abort,
  616. eh_host_reset_handler: mts_scsi_host_reset,
  617. sg_tablesize: SG_ALL,
  618. can_queue: 1,
  619. this_id: -1,
  620. cmd_per_lun: 1,
  621. present: 0,
  622. unchecked_isa_dma: FALSE,
  623. use_clustering: TRUE,
  624. use_new_eh_code: TRUE,
  625. emulated: TRUE
  626. };
  627. /* USB layer driver interface implementation */
  628. static void mts_usb_disconnect (struct usb_device *dev, void *ptr)
  629. {
  630. struct mts_desc* to_remove = (struct mts_desc*)ptr;
  631. MTS_DEBUG_GOT_HERE();
  632. /* leave the list - lock it */
  633. down(&mts_list_semaphore);
  634. mts_remove_nolock(to_remove);
  635. up(&mts_list_semaphore);
  636. }
  637. struct vendor_product
  638. {
  639. char* name;
  640. enum
  641. {
  642. mts_sup_unknown=0,
  643. mts_sup_alpha,
  644. mts_sup_full
  645. }
  646. support_status;
  647. } ;
  648. /* These are taken from the msmUSB.inf file on the Windows driver CD */
  649. const static struct vendor_product mts_supported_products[] =
  650. {
  651. { "Phantom 336CX", mts_sup_unknown},
  652. { "Phantom 336CX", mts_sup_unknown},
  653. { "Scanmaker X6", mts_sup_alpha},
  654. { "Phantom C6", mts_sup_unknown},
  655. { "Phantom 336CX", mts_sup_unknown},
  656. { "ScanMaker V6USL", mts_sup_unknown},
  657. { "ScanMaker V6USL", mts_sup_unknown},
  658. { "Scanmaker V6UL", mts_sup_unknown},
  659. { "Scanmaker V6UPL", mts_sup_alpha},
  660. };
  661. /* The entries of microtek_table must correspond, line-by-line to
  662.    the entries of mts_supported_products[]. */
  663. static struct usb_device_id mts_usb_ids [] =
  664. {
  665. { USB_DEVICE(0x4ce, 0x0300) },
  666. { USB_DEVICE(0x5da, 0x0094) },
  667. { USB_DEVICE(0x5da, 0x0099) },
  668. { USB_DEVICE(0x5da, 0x009a) },
  669. { USB_DEVICE(0x5da, 0x00a0) },
  670. { USB_DEVICE(0x5da, 0x00a3) },
  671. { USB_DEVICE(0x5da, 0x80a3) },
  672. { USB_DEVICE(0x5da, 0x80ac) },
  673. { USB_DEVICE(0x5da, 0x00b6) },
  674. { } /* Terminating entry */
  675. };
  676. MODULE_DEVICE_TABLE (usb, mts_usb_ids);
  677. static void * mts_usb_probe (struct usb_device *dev, unsigned int interface,
  678.      const struct usb_device_id *id)
  679. {
  680. int i;
  681. int result;
  682. int ep_out = -1;
  683. int ep_in_set[3]; /* this will break if we have more than three endpoints
  684.    which is why we check */
  685. int *ep_in_current = ep_in_set;
  686. struct mts_desc * new_desc;
  687. struct vendor_product const* p;
  688. /* the altsettting 0 on the interface we're probing */
  689. struct usb_interface_descriptor *altsetting;
  690. MTS_DEBUG_GOT_HERE();
  691. MTS_DEBUG( "usb-device descriptor at %xn", (int)dev );
  692. MTS_DEBUG( "product id = 0x%x, vendor id = 0x%xn",
  693.    (int)dev->descriptor.idProduct,
  694.    (int)dev->descriptor.idVendor );
  695. MTS_DEBUG_GOT_HERE();
  696. p = &mts_supported_products[id - mts_usb_ids];
  697. MTS_DEBUG_GOT_HERE();
  698. MTS_DEBUG( "found model %sn", p->name );
  699. if ( p->support_status != mts_sup_full )
  700. MTS_MESSAGE( "model %s is not known to be fully supported, reports welcome!n",
  701.      p->name );
  702. /* the altsettting 0 on the interface we're probing */
  703. altsetting =
  704. &(dev->actconfig->interface[interface].altsetting[0]);
  705. /* Check if the config is sane */
  706. if ( altsetting->bNumEndpoints != MTS_EP_TOTAL ) {
  707. MTS_WARNING( "expecting %d got %d endpoints! Bailing out.n",
  708.      (int)MTS_EP_TOTAL, (int)altsetting->bNumEndpoints );
  709. return NULL;
  710. }
  711. for( i = 0; i < altsetting->bNumEndpoints; i++ ) {
  712. if ((altsetting->endpoint[i].bmAttributes &
  713.      USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
  714. MTS_WARNING( "can only deal with bulk endpoints; endpoint %d is not bulk.n",
  715.      (int)altsetting->endpoint[i].bEndpointAddress );
  716. } else {
  717. if (altsetting->endpoint[i].bEndpointAddress &
  718.     USB_DIR_IN)
  719. *ep_in_current++
  720. = altsetting->endpoint[i].bEndpointAddress &
  721. USB_ENDPOINT_NUMBER_MASK;
  722. else {
  723. if ( ep_out != -1 ) {
  724. MTS_WARNING( "can only deal with one output endpoints. Bailing out." );
  725. return NULL;
  726. }
  727. ep_out = altsetting->endpoint[i].bEndpointAddress &
  728. USB_ENDPOINT_NUMBER_MASK;
  729. }
  730. }
  731. }
  732. if ( ep_out == -1 ) {
  733. MTS_WARNING( "couldn't find an output bulk endpoint. Bailing out.n" );
  734. return NULL;
  735. }
  736. /* I don't understand the following fully (it's from usb-storage) -- John */
  737. /* set the interface -- STALL is an acceptable response here */
  738. result = usb_set_interface(dev, altsetting->bInterfaceNumber, 0);
  739. MTS_DEBUG("usb_set_interface returned %d.n",result);
  740. switch( result )
  741. {
  742. case 0: /* no error */
  743. break;
  744. case -EPIPE:
  745. usb_clear_halt(dev, usb_sndctrlpipe(dev, 0));
  746. MTS_DEBUG( "clearing clearing stall on control interfacen" );
  747. break;
  748. default:
  749. MTS_DEBUG( "unknown error %d from usb_set_interfacen",
  750. (int)result );
  751.   return NULL;
  752. }
  753. /* allocating a new descriptor */
  754. new_desc = (struct mts_desc *)kmalloc(sizeof(struct mts_desc), GFP_KERNEL);
  755. if (new_desc == NULL)
  756. {
  757. MTS_ERROR("couldn't allocate scanner desc, bailing out!n");
  758. return NULL;
  759. }
  760. /* As done by usb_alloc_urb */
  761. memset( new_desc, 0, sizeof(*new_desc) );
  762. spin_lock_init(&new_desc->urb.lock);
  763. /* initialising that descriptor */
  764. new_desc->usb_dev = dev;
  765. new_desc->interface = interface;
  766. init_MUTEX(&new_desc->lock);
  767. if(mts_list){
  768. new_desc->host_number = mts_list->host_number+1;
  769. } else {
  770. new_desc->host_number = 0;
  771. }
  772. /* endpoints */
  773. new_desc->ep_out = ep_out;
  774. new_desc->ep_response = ep_in_set[0];
  775. new_desc->ep_image = ep_in_set[1];
  776. if ( new_desc->ep_out != MTS_EP_OUT )
  777. MTS_WARNING( "will this work? Command EP is not usually %dn",
  778.      (int)new_desc->ep_out );
  779. if ( new_desc->ep_response != MTS_EP_RESPONSE )
  780. MTS_WARNING( "will this work? Response EP is not usually %dn",
  781.      (int)new_desc->ep_response );
  782. if ( new_desc->ep_image != MTS_EP_IMAGE )
  783. MTS_WARNING( "will this work? Image data EP is not usually %dn",
  784.      (int)new_desc->ep_image );
  785. /* Initialize the host template based on the default one */
  786. memcpy(&(new_desc->ctempl), &mts_scsi_host_template, sizeof(mts_scsi_host_template));
  787. /* HACK from usb-storage - this is needed for scsi detection */
  788. (struct mts_desc *)new_desc->ctempl.proc_dir = new_desc; /* FIXME */
  789. MTS_DEBUG("registering SCSI modulen");
  790. new_desc->ctempl.module = THIS_MODULE;
  791. result = scsi_register_module(MODULE_SCSI_HA, &(new_desc->ctempl));
  792. /* Will get hit back in microtek_detect by this func */
  793. if ( result )
  794. {
  795. MTS_ERROR( "error %d from scsi_register_module! Help!n",
  796.    (int)result );
  797. /* FIXME: need more cleanup? */
  798. kfree( new_desc );
  799. return NULL;
  800. }
  801. MTS_DEBUG_GOT_HERE();
  802. /* FIXME: the bomb is armed, must the host be registered under lock ? */
  803. /* join the list - lock it */
  804. down(&mts_list_semaphore);
  805. mts_add_nolock( new_desc );
  806. up(&mts_list_semaphore);
  807. MTS_DEBUG("completed probe and exiting happilyn");
  808. return (void *)new_desc;
  809. }
  810. /* get us noticed by the rest of the kernel */
  811. int __init microtek_drv_init(void)
  812. {
  813. int result;
  814. MTS_DEBUG_GOT_HERE();
  815. init_MUTEX(&mts_list_semaphore);
  816. if ((result = usb_register(&mts_usb_driver)) < 0) {
  817. MTS_DEBUG("usb_register returned %dn", result );
  818. return -1;
  819. } else {
  820. MTS_DEBUG("driver registered.n");
  821. }
  822. info(DRIVER_VERSION ":" DRIVER_DESC);
  823. return 0;
  824. }
  825. void __exit microtek_drv_exit(void)
  826. {
  827. struct mts_desc* next;
  828. MTS_DEBUG_GOT_HERE();
  829. usb_deregister(&mts_usb_driver);
  830. down(&mts_list_semaphore);
  831. while (mts_list) {
  832. /* keep track of where the next one is */
  833. next = mts_list->next;
  834. mts_remove_nolock( mts_list );
  835. /* advance the list pointer */
  836. mts_list = next;
  837. }
  838. up(&mts_list_semaphore);
  839. }
  840. module_init(microtek_drv_init);
  841. module_exit(microtek_drv_exit);
  842. MODULE_AUTHOR( DRIVER_AUTHOR );
  843. MODULE_DESCRIPTION( DRIVER_DESC );
  844. MODULE_LICENSE("GPL");