ub.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  1. /*
  2. * The low performance USB storage driver (ub).
  3. *
  4. * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  5. * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
  6. *
  7. * This work is a part of Linux kernel, is derived from it,
  8. * and is not licensed separately. See file COPYING for details.
  9. *
  10. * TODO (sorted by decreasing priority)
  11. * -- set readonly flag for CDs, set removable flag for CF readers
  12. * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
  13. * -- verify the 13 conditions and do bulk resets
  14. * -- highmem
  15. * -- move top_sense and work_bcs into separate allocations (if they survive)
  16. * for cache purists and esoteric architectures.
  17. * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
  18. * -- prune comments, they are too volumnous
  19. * -- Resove XXX's
  20. * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb_usual.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/timer.h>
  28. #include <scsi/scsi.h>
  29. #define DRV_NAME "ub"
  30. #define DEVFS_NAME DRV_NAME
  31. #define UB_MAJOR 180
  32. /*
  33. * The command state machine is the key model for understanding of this driver.
  34. *
  35. * The general rule is that all transitions are done towards the bottom
  36. * of the diagram, thus preventing any loops.
  37. *
  38. * An exception to that is how the STAT state is handled. A counter allows it
  39. * to be re-entered along the path marked with [C].
  40. *
  41. * +--------+
  42. * ! INIT !
  43. * +--------+
  44. * !
  45. * ub_scsi_cmd_start fails ->--------------------------------------\
  46. * ! !
  47. * V !
  48. * +--------+ !
  49. * ! CMD ! !
  50. * +--------+ !
  51. * ! +--------+ !
  52. * was -EPIPE -->-------------------------------->! CLEAR ! !
  53. * ! +--------+ !
  54. * ! ! !
  55. * was error -->------------------------------------- ! --------->\
  56. * ! ! !
  57. * /--<-- cmd->dir == NONE ? ! !
  58. * ! ! ! !
  59. * ! V ! !
  60. * ! +--------+ ! !
  61. * ! ! DATA ! ! !
  62. * ! +--------+ ! !
  63. * ! ! +---------+ ! !
  64. * ! was -EPIPE -->--------------->! CLR2STS ! ! !
  65. * ! ! +---------+ ! !
  66. * ! ! ! ! !
  67. * ! ! was error -->---- ! --------->\
  68. * ! was error -->--------------------- ! ------------- ! --------->\
  69. * ! ! ! ! !
  70. * ! V ! ! !
  71. * \--->+--------+ ! ! !
  72. * ! STAT !<--------------------------/ ! !
  73. * /--->+--------+ ! !
  74. * ! ! ! !
  75. * [C] was -EPIPE -->-----------\ ! !
  76. * ! ! ! ! !
  77. * +<---- len == 0 ! ! !
  78. * ! ! ! ! !
  79. * ! was error -->--------------------------------------!---------->\
  80. * ! ! ! ! !
  81. * +<---- bad CSW ! ! !
  82. * +<---- bad tag ! ! !
  83. * ! ! V ! !
  84. * ! ! +--------+ ! !
  85. * ! ! ! CLRRS ! ! !
  86. * ! ! +--------+ ! !
  87. * ! ! ! ! !
  88. * \------- ! --------------------[C]--------\ ! !
  89. * ! ! ! !
  90. * cmd->error---\ +--------+ ! !
  91. * ! +--------------->! SENSE !<----------/ !
  92. * STAT_FAIL----/ +--------+ !
  93. * ! ! V
  94. * ! V +--------+
  95. * \--------------------------------\--------------------->! DONE !
  96. * +--------+
  97. */
  98. /*
  99. * This many LUNs per USB device.
  100. * Every one of them takes a host, see UB_MAX_HOSTS.
  101. */
  102. #define UB_MAX_LUNS 9
  103. /*
  104. */
  105. #define UB_PARTS_PER_LUN 8
  106. #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
  107. #define UB_SENSE_SIZE 18
  108. /*
  109. */
  110. /* command block wrapper */
  111. struct bulk_cb_wrap {
  112. __le32 Signature; /* contains 'USBC' */
  113. u32 Tag; /* unique per command id */
  114. __le32 DataTransferLength; /* size of data */
  115. u8 Flags; /* direction in bit 0 */
  116. u8 Lun; /* LUN */
  117. u8 Length; /* of of the CDB */
  118. u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
  119. };
  120. #define US_BULK_CB_WRAP_LEN 31
  121. #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
  122. #define US_BULK_FLAG_IN 1
  123. #define US_BULK_FLAG_OUT 0
  124. /* command status wrapper */
  125. struct bulk_cs_wrap {
  126. __le32 Signature; /* should = 'USBS' */
  127. u32 Tag; /* same as original command */
  128. __le32 Residue; /* amount not transferred */
  129. u8 Status; /* see below */
  130. };
  131. #define US_BULK_CS_WRAP_LEN 13
  132. #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
  133. #define US_BULK_STAT_OK 0
  134. #define US_BULK_STAT_FAIL 1
  135. #define US_BULK_STAT_PHASE 2
  136. /* bulk-only class specific requests */
  137. #define US_BULK_RESET_REQUEST 0xff
  138. #define US_BULK_GET_MAX_LUN 0xfe
  139. /*
  140. */
  141. struct ub_dev;
  142. #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
  143. #define UB_MAX_SECTORS 64
  144. /*
  145. * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
  146. * even if a webcam hogs the bus, but some devices need time to spin up.
  147. */
  148. #define UB_URB_TIMEOUT (HZ*2)
  149. #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
  150. #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
  151. #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
  152. /*
  153. * An instance of a SCSI command in transit.
  154. */
  155. #define UB_DIR_NONE 0
  156. #define UB_DIR_READ 1
  157. #define UB_DIR_ILLEGAL2 2
  158. #define UB_DIR_WRITE 3
  159. #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
  160. (((c)==UB_DIR_READ)? 'r': 'n'))
  161. enum ub_scsi_cmd_state {
  162. UB_CMDST_INIT, /* Initial state */
  163. UB_CMDST_CMD, /* Command submitted */
  164. UB_CMDST_DATA, /* Data phase */
  165. UB_CMDST_CLR2STS, /* Clearing before requesting status */
  166. UB_CMDST_STAT, /* Status phase */
  167. UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
  168. UB_CMDST_CLRRS, /* Clearing before retrying status */
  169. UB_CMDST_SENSE, /* Sending Request Sense */
  170. UB_CMDST_DONE /* Final state */
  171. };
  172. struct ub_scsi_cmd {
  173. unsigned char cdb[UB_MAX_CDB_SIZE];
  174. unsigned char cdb_len;
  175. unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
  176. enum ub_scsi_cmd_state state;
  177. unsigned int tag;
  178. struct ub_scsi_cmd *next;
  179. int error; /* Return code - valid upon done */
  180. unsigned int act_len; /* Return size */
  181. unsigned char key, asc, ascq; /* May be valid if error==-EIO */
  182. int stat_count; /* Retries getting status. */
  183. unsigned int len; /* Requested length */
  184. unsigned int current_sg;
  185. unsigned int nsg; /* sgv[nsg] */
  186. struct scatterlist sgv[UB_MAX_REQ_SG];
  187. struct ub_lun *lun;
  188. void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
  189. void *back;
  190. };
  191. struct ub_request {
  192. struct request *rq;
  193. unsigned int current_try;
  194. unsigned int nsg; /* sgv[nsg] */
  195. struct scatterlist sgv[UB_MAX_REQ_SG];
  196. };
  197. /*
  198. */
  199. struct ub_capacity {
  200. unsigned long nsec; /* Linux size - 512 byte sectors */
  201. unsigned int bsize; /* Linux hardsect_size */
  202. unsigned int bshift; /* Shift between 512 and hard sects */
  203. };
  204. /*
  205. * This is a direct take-off from linux/include/completion.h
  206. * The difference is that I do not wait on this thing, just poll.
  207. * When I want to wait (ub_probe), I just use the stock completion.
  208. *
  209. * Note that INIT_COMPLETION takes no lock. It is correct. But why
  210. * in the bloody hell that thing takes struct instead of pointer to struct
  211. * is quite beyond me. I just copied it from the stock completion.
  212. */
  213. struct ub_completion {
  214. unsigned int done;
  215. spinlock_t lock;
  216. };
  217. static inline void ub_init_completion(struct ub_completion *x)
  218. {
  219. x->done = 0;
  220. spin_lock_init(&x->lock);
  221. }
  222. #define UB_INIT_COMPLETION(x) ((x).done = 0)
  223. static void ub_complete(struct ub_completion *x)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&x->lock, flags);
  227. x->done++;
  228. spin_unlock_irqrestore(&x->lock, flags);
  229. }
  230. static int ub_is_completed(struct ub_completion *x)
  231. {
  232. unsigned long flags;
  233. int ret;
  234. spin_lock_irqsave(&x->lock, flags);
  235. ret = x->done;
  236. spin_unlock_irqrestore(&x->lock, flags);
  237. return ret;
  238. }
  239. /*
  240. */
  241. struct ub_scsi_cmd_queue {
  242. int qlen, qmax;
  243. struct ub_scsi_cmd *head, *tail;
  244. };
  245. /*
  246. * The block device instance (one per LUN).
  247. */
  248. struct ub_lun {
  249. struct ub_dev *udev;
  250. struct list_head link;
  251. struct gendisk *disk;
  252. int id; /* Host index */
  253. int num; /* LUN number */
  254. char name[16];
  255. int changed; /* Media was changed */
  256. int removable;
  257. int readonly;
  258. struct ub_request urq;
  259. /* Use Ingo's mempool if or when we have more than one command. */
  260. /*
  261. * Currently we never need more than one command for the whole device.
  262. * However, giving every LUN a command is a cheap and automatic way
  263. * to enforce fairness between them.
  264. */
  265. int cmda[1];
  266. struct ub_scsi_cmd cmdv[1];
  267. struct ub_capacity capacity;
  268. };
  269. /*
  270. * The USB device instance.
  271. */
  272. struct ub_dev {
  273. spinlock_t *lock;
  274. atomic_t poison; /* The USB device is disconnected */
  275. int openc; /* protected by ub_lock! */
  276. /* kref is too implicit for our taste */
  277. int reset; /* Reset is running */
  278. unsigned int tagcnt;
  279. char name[12];
  280. struct usb_device *dev;
  281. struct usb_interface *intf;
  282. struct list_head luns;
  283. unsigned int send_bulk_pipe; /* cached pipe values */
  284. unsigned int recv_bulk_pipe;
  285. unsigned int send_ctrl_pipe;
  286. unsigned int recv_ctrl_pipe;
  287. struct tasklet_struct tasklet;
  288. struct ub_scsi_cmd_queue cmd_queue;
  289. struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
  290. unsigned char top_sense[UB_SENSE_SIZE];
  291. struct ub_completion work_done;
  292. struct urb work_urb;
  293. struct timer_list work_timer;
  294. int last_pipe; /* What might need clearing */
  295. __le32 signature; /* Learned signature */
  296. struct bulk_cb_wrap work_bcb;
  297. struct bulk_cs_wrap work_bcs;
  298. struct usb_ctrlrequest work_cr;
  299. struct work_struct reset_work;
  300. wait_queue_head_t reset_wait;
  301. int sg_stat[6];
  302. };
  303. /*
  304. */
  305. static void ub_cleanup(struct ub_dev *sc);
  306. static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
  307. static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
  308. struct ub_scsi_cmd *cmd, struct ub_request *urq);
  309. static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
  310. struct ub_scsi_cmd *cmd, struct ub_request *urq);
  311. static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  312. static void ub_end_rq(struct request *rq, int uptodate);
  313. static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
  314. struct ub_request *urq, struct ub_scsi_cmd *cmd);
  315. static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  316. static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
  317. static void ub_scsi_action(unsigned long _dev);
  318. static void ub_scsi_dispatch(struct ub_dev *sc);
  319. static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  320. static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  321. static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
  322. static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  323. static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  324. static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  325. static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
  326. static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  327. int stalled_pipe);
  328. static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
  329. static void ub_reset_enter(struct ub_dev *sc, int try);
  330. static void ub_reset_task(void *arg);
  331. static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
  332. static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
  333. struct ub_capacity *ret);
  334. static int ub_sync_reset(struct ub_dev *sc);
  335. static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
  336. static int ub_probe_lun(struct ub_dev *sc, int lnum);
  337. /*
  338. */
  339. #ifdef CONFIG_USB_LIBUSUAL
  340. #define ub_usb_ids storage_usb_ids
  341. #else
  342. static struct usb_device_id ub_usb_ids[] = {
  343. { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(usb, ub_usb_ids);
  347. #endif /* CONFIG_USB_LIBUSUAL */
  348. /*
  349. * Find me a way to identify "next free minor" for add_disk(),
  350. * and the array disappears the next day. However, the number of
  351. * hosts has something to do with the naming and /proc/partitions.
  352. * This has to be thought out in detail before changing.
  353. * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
  354. */
  355. #define UB_MAX_HOSTS 26
  356. static char ub_hostv[UB_MAX_HOSTS];
  357. #define UB_QLOCK_NUM 5
  358. static spinlock_t ub_qlockv[UB_QLOCK_NUM];
  359. static int ub_qlock_next = 0;
  360. static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
  361. /*
  362. * The id allocator.
  363. *
  364. * This also stores the host for indexing by minor, which is somewhat dirty.
  365. */
  366. static int ub_id_get(void)
  367. {
  368. unsigned long flags;
  369. int i;
  370. spin_lock_irqsave(&ub_lock, flags);
  371. for (i = 0; i < UB_MAX_HOSTS; i++) {
  372. if (ub_hostv[i] == 0) {
  373. ub_hostv[i] = 1;
  374. spin_unlock_irqrestore(&ub_lock, flags);
  375. return i;
  376. }
  377. }
  378. spin_unlock_irqrestore(&ub_lock, flags);
  379. return -1;
  380. }
  381. static void ub_id_put(int id)
  382. {
  383. unsigned long flags;
  384. if (id < 0 || id >= UB_MAX_HOSTS) {
  385. printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
  386. return;
  387. }
  388. spin_lock_irqsave(&ub_lock, flags);
  389. if (ub_hostv[id] == 0) {
  390. spin_unlock_irqrestore(&ub_lock, flags);
  391. printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
  392. return;
  393. }
  394. ub_hostv[id] = 0;
  395. spin_unlock_irqrestore(&ub_lock, flags);
  396. }
  397. /*
  398. * This is necessitated by the fact that blk_cleanup_queue does not
  399. * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
  400. * Since our blk_init_queue() passes a spinlock common with ub_dev,
  401. * we have life time issues when ub_cleanup frees ub_dev.
  402. */
  403. static spinlock_t *ub_next_lock(void)
  404. {
  405. unsigned long flags;
  406. spinlock_t *ret;
  407. spin_lock_irqsave(&ub_lock, flags);
  408. ret = &ub_qlockv[ub_qlock_next];
  409. ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
  410. spin_unlock_irqrestore(&ub_lock, flags);
  411. return ret;
  412. }
  413. /*
  414. * Downcount for deallocation. This rides on two assumptions:
  415. * - once something is poisoned, its refcount cannot grow
  416. * - opens cannot happen at this time (del_gendisk was done)
  417. * If the above is true, we can drop the lock, which we need for
  418. * blk_cleanup_queue(): the silly thing may attempt to sleep.
  419. * [Actually, it never needs to sleep for us, but it calls might_sleep()]
  420. */
  421. static void ub_put(struct ub_dev *sc)
  422. {
  423. unsigned long flags;
  424. spin_lock_irqsave(&ub_lock, flags);
  425. --sc->openc;
  426. if (sc->openc == 0 && atomic_read(&sc->poison)) {
  427. spin_unlock_irqrestore(&ub_lock, flags);
  428. ub_cleanup(sc);
  429. } else {
  430. spin_unlock_irqrestore(&ub_lock, flags);
  431. }
  432. }
  433. /*
  434. * Final cleanup and deallocation.
  435. */
  436. static void ub_cleanup(struct ub_dev *sc)
  437. {
  438. struct list_head *p;
  439. struct ub_lun *lun;
  440. request_queue_t *q;
  441. while (!list_empty(&sc->luns)) {
  442. p = sc->luns.next;
  443. lun = list_entry(p, struct ub_lun, link);
  444. list_del(p);
  445. /* I don't think queue can be NULL. But... Stolen from sx8.c */
  446. if ((q = lun->disk->queue) != NULL)
  447. blk_cleanup_queue(q);
  448. /*
  449. * If we zero disk->private_data BEFORE put_disk, we have
  450. * to check for NULL all over the place in open, release,
  451. * check_media and revalidate, because the block level
  452. * semaphore is well inside the put_disk.
  453. * But we cannot zero after the call, because *disk is gone.
  454. * The sd.c is blatantly racy in this area.
  455. */
  456. /* disk->private_data = NULL; */
  457. put_disk(lun->disk);
  458. lun->disk = NULL;
  459. ub_id_put(lun->id);
  460. kfree(lun);
  461. }
  462. usb_set_intfdata(sc->intf, NULL);
  463. usb_put_intf(sc->intf);
  464. usb_put_dev(sc->dev);
  465. kfree(sc);
  466. }
  467. /*
  468. * The "command allocator".
  469. */
  470. static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
  471. {
  472. struct ub_scsi_cmd *ret;
  473. if (lun->cmda[0])
  474. return NULL;
  475. ret = &lun->cmdv[0];
  476. lun->cmda[0] = 1;
  477. return ret;
  478. }
  479. static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
  480. {
  481. if (cmd != &lun->cmdv[0]) {
  482. printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
  483. lun->name, cmd);
  484. return;
  485. }
  486. if (!lun->cmda[0]) {
  487. printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
  488. return;
  489. }
  490. lun->cmda[0] = 0;
  491. }
  492. /*
  493. * The command queue.
  494. */
  495. static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  496. {
  497. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  498. if (t->qlen++ == 0) {
  499. t->head = cmd;
  500. t->tail = cmd;
  501. } else {
  502. t->tail->next = cmd;
  503. t->tail = cmd;
  504. }
  505. if (t->qlen > t->qmax)
  506. t->qmax = t->qlen;
  507. }
  508. static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  509. {
  510. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  511. if (t->qlen++ == 0) {
  512. t->head = cmd;
  513. t->tail = cmd;
  514. } else {
  515. cmd->next = t->head;
  516. t->head = cmd;
  517. }
  518. if (t->qlen > t->qmax)
  519. t->qmax = t->qlen;
  520. }
  521. static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
  522. {
  523. struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
  524. struct ub_scsi_cmd *cmd;
  525. if (t->qlen == 0)
  526. return NULL;
  527. if (--t->qlen == 0)
  528. t->tail = NULL;
  529. cmd = t->head;
  530. t->head = cmd->next;
  531. cmd->next = NULL;
  532. return cmd;
  533. }
  534. #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
  535. /*
  536. * The request function is our main entry point
  537. */
  538. static void ub_request_fn(request_queue_t *q)
  539. {
  540. struct ub_lun *lun = q->queuedata;
  541. struct request *rq;
  542. while ((rq = elv_next_request(q)) != NULL) {
  543. if (ub_request_fn_1(lun, rq) != 0) {
  544. blk_stop_queue(q);
  545. break;
  546. }
  547. }
  548. }
  549. static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
  550. {
  551. struct ub_dev *sc = lun->udev;
  552. struct ub_scsi_cmd *cmd;
  553. struct ub_request *urq;
  554. int n_elem;
  555. if (atomic_read(&sc->poison) || lun->changed) {
  556. blkdev_dequeue_request(rq);
  557. ub_end_rq(rq, 0);
  558. return 0;
  559. }
  560. if (lun->urq.rq != NULL)
  561. return -1;
  562. if ((cmd = ub_get_cmd(lun)) == NULL)
  563. return -1;
  564. memset(cmd, 0, sizeof(struct ub_scsi_cmd));
  565. blkdev_dequeue_request(rq);
  566. urq = &lun->urq;
  567. memset(urq, 0, sizeof(struct ub_request));
  568. urq->rq = rq;
  569. /*
  570. * get scatterlist from block layer
  571. */
  572. n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
  573. if (n_elem < 0) {
  574. /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
  575. printk(KERN_INFO "%s: failed request map (%d)\n",
  576. lun->name, n_elem);
  577. goto drop;
  578. }
  579. if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
  580. printk(KERN_WARNING "%s: request with %d segments\n",
  581. lun->name, n_elem);
  582. goto drop;
  583. }
  584. urq->nsg = n_elem;
  585. sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
  586. if (blk_pc_request(rq)) {
  587. ub_cmd_build_packet(sc, lun, cmd, urq);
  588. } else {
  589. ub_cmd_build_block(sc, lun, cmd, urq);
  590. }
  591. cmd->state = UB_CMDST_INIT;
  592. cmd->lun = lun;
  593. cmd->done = ub_rw_cmd_done;
  594. cmd->back = urq;
  595. cmd->tag = sc->tagcnt++;
  596. if (ub_submit_scsi(sc, cmd) != 0)
  597. goto drop;
  598. return 0;
  599. drop:
  600. ub_put_cmd(lun, cmd);
  601. ub_end_rq(rq, 0);
  602. return 0;
  603. }
  604. static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
  605. struct ub_scsi_cmd *cmd, struct ub_request *urq)
  606. {
  607. struct request *rq = urq->rq;
  608. unsigned int block, nblks;
  609. if (rq_data_dir(rq) == WRITE)
  610. cmd->dir = UB_DIR_WRITE;
  611. else
  612. cmd->dir = UB_DIR_READ;
  613. cmd->nsg = urq->nsg;
  614. memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
  615. /*
  616. * build the command
  617. *
  618. * The call to blk_queue_hardsect_size() guarantees that request
  619. * is aligned, but it is given in terms of 512 byte units, always.
  620. */
  621. block = rq->sector >> lun->capacity.bshift;
  622. nblks = rq->nr_sectors >> lun->capacity.bshift;
  623. cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
  624. /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
  625. cmd->cdb[2] = block >> 24;
  626. cmd->cdb[3] = block >> 16;
  627. cmd->cdb[4] = block >> 8;
  628. cmd->cdb[5] = block;
  629. cmd->cdb[7] = nblks >> 8;
  630. cmd->cdb[8] = nblks;
  631. cmd->cdb_len = 10;
  632. cmd->len = rq->nr_sectors * 512;
  633. }
  634. static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
  635. struct ub_scsi_cmd *cmd, struct ub_request *urq)
  636. {
  637. struct request *rq = urq->rq;
  638. if (rq->data_len == 0) {
  639. cmd->dir = UB_DIR_NONE;
  640. } else {
  641. if (rq_data_dir(rq) == WRITE)
  642. cmd->dir = UB_DIR_WRITE;
  643. else
  644. cmd->dir = UB_DIR_READ;
  645. }
  646. cmd->nsg = urq->nsg;
  647. memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
  648. memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
  649. cmd->cdb_len = rq->cmd_len;
  650. cmd->len = rq->data_len;
  651. }
  652. static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  653. {
  654. struct ub_lun *lun = cmd->lun;
  655. struct ub_request *urq = cmd->back;
  656. struct request *rq;
  657. int uptodate;
  658. rq = urq->rq;
  659. if (cmd->error == 0) {
  660. uptodate = 1;
  661. if (blk_pc_request(rq)) {
  662. if (cmd->act_len >= rq->data_len)
  663. rq->data_len = 0;
  664. else
  665. rq->data_len -= cmd->act_len;
  666. }
  667. } else {
  668. uptodate = 0;
  669. if (blk_pc_request(rq)) {
  670. /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
  671. memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
  672. rq->sense_len = UB_SENSE_SIZE;
  673. if (sc->top_sense[0] != 0)
  674. rq->errors = SAM_STAT_CHECK_CONDITION;
  675. else
  676. rq->errors = DID_ERROR << 16;
  677. } else {
  678. if (cmd->error == -EIO) {
  679. if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
  680. return;
  681. }
  682. }
  683. }
  684. urq->rq = NULL;
  685. ub_put_cmd(lun, cmd);
  686. ub_end_rq(rq, uptodate);
  687. blk_start_queue(lun->disk->queue);
  688. }
  689. static void ub_end_rq(struct request *rq, int uptodate)
  690. {
  691. end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
  692. end_that_request_last(rq, uptodate);
  693. }
  694. static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
  695. struct ub_request *urq, struct ub_scsi_cmd *cmd)
  696. {
  697. if (atomic_read(&sc->poison))
  698. return -ENXIO;
  699. ub_reset_enter(sc, urq->current_try);
  700. if (urq->current_try >= 3)
  701. return -EIO;
  702. urq->current_try++;
  703. /* Remove this if anyone complains of flooding. */
  704. printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
  705. "[sense %x %02x %02x] retry %d\n",
  706. sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
  707. cmd->key, cmd->asc, cmd->ascq, urq->current_try);
  708. memset(cmd, 0, sizeof(struct ub_scsi_cmd));
  709. ub_cmd_build_block(sc, lun, cmd, urq);
  710. cmd->state = UB_CMDST_INIT;
  711. cmd->lun = lun;
  712. cmd->done = ub_rw_cmd_done;
  713. cmd->back = urq;
  714. cmd->tag = sc->tagcnt++;
  715. #if 0 /* Wasteful */
  716. return ub_submit_scsi(sc, cmd);
  717. #else
  718. ub_cmdq_add(sc, cmd);
  719. return 0;
  720. #endif
  721. }
  722. /*
  723. * Submit a regular SCSI operation (not an auto-sense).
  724. *
  725. * The Iron Law of Good Submit Routine is:
  726. * Zero return - callback is done, Nonzero return - callback is not done.
  727. * No exceptions.
  728. *
  729. * Host is assumed locked.
  730. */
  731. static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  732. {
  733. if (cmd->state != UB_CMDST_INIT ||
  734. (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
  735. return -EINVAL;
  736. }
  737. ub_cmdq_add(sc, cmd);
  738. /*
  739. * We can call ub_scsi_dispatch(sc) right away here, but it's a little
  740. * safer to jump to a tasklet, in case upper layers do something silly.
  741. */
  742. tasklet_schedule(&sc->tasklet);
  743. return 0;
  744. }
  745. /*
  746. * Submit the first URB for the queued command.
  747. * This function does not deal with queueing in any way.
  748. */
  749. static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  750. {
  751. struct bulk_cb_wrap *bcb;
  752. int rc;
  753. bcb = &sc->work_bcb;
  754. /*
  755. * ``If the allocation length is eighteen or greater, and a device
  756. * server returns less than eithteen bytes of data, the application
  757. * client should assume that the bytes not transferred would have been
  758. * zeroes had the device server returned those bytes.''
  759. *
  760. * We zero sense for all commands so that when a packet request
  761. * fails it does not return a stale sense.
  762. */
  763. memset(&sc->top_sense, 0, UB_SENSE_SIZE);
  764. /* set up the command wrapper */
  765. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  766. bcb->Tag = cmd->tag; /* Endianness is not important */
  767. bcb->DataTransferLength = cpu_to_le32(cmd->len);
  768. bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
  769. bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
  770. bcb->Length = cmd->cdb_len;
  771. /* copy the command payload */
  772. memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
  773. UB_INIT_COMPLETION(sc->work_done);
  774. sc->last_pipe = sc->send_bulk_pipe;
  775. usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
  776. bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
  777. /* Fill what we shouldn't be filling, because usb-storage did so. */
  778. sc->work_urb.actual_length = 0;
  779. sc->work_urb.error_count = 0;
  780. sc->work_urb.status = 0;
  781. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  782. /* XXX Clear stalls */
  783. ub_complete(&sc->work_done);
  784. return rc;
  785. }
  786. sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
  787. add_timer(&sc->work_timer);
  788. cmd->state = UB_CMDST_CMD;
  789. return 0;
  790. }
  791. /*
  792. * Timeout handler.
  793. */
  794. static void ub_urb_timeout(unsigned long arg)
  795. {
  796. struct ub_dev *sc = (struct ub_dev *) arg;
  797. unsigned long flags;
  798. spin_lock_irqsave(sc->lock, flags);
  799. if (!ub_is_completed(&sc->work_done))
  800. usb_unlink_urb(&sc->work_urb);
  801. spin_unlock_irqrestore(sc->lock, flags);
  802. }
  803. /*
  804. * Completion routine for the work URB.
  805. *
  806. * This can be called directly from usb_submit_urb (while we have
  807. * the sc->lock taken) and from an interrupt (while we do NOT have
  808. * the sc->lock taken). Therefore, bounce this off to a tasklet.
  809. */
  810. static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
  811. {
  812. struct ub_dev *sc = urb->context;
  813. ub_complete(&sc->work_done);
  814. tasklet_schedule(&sc->tasklet);
  815. }
  816. static void ub_scsi_action(unsigned long _dev)
  817. {
  818. struct ub_dev *sc = (struct ub_dev *) _dev;
  819. unsigned long flags;
  820. spin_lock_irqsave(sc->lock, flags);
  821. ub_scsi_dispatch(sc);
  822. spin_unlock_irqrestore(sc->lock, flags);
  823. }
  824. static void ub_scsi_dispatch(struct ub_dev *sc)
  825. {
  826. struct ub_scsi_cmd *cmd;
  827. int rc;
  828. while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
  829. if (cmd->state == UB_CMDST_DONE) {
  830. ub_cmdq_pop(sc);
  831. (*cmd->done)(sc, cmd);
  832. } else if (cmd->state == UB_CMDST_INIT) {
  833. if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
  834. break;
  835. cmd->error = rc;
  836. cmd->state = UB_CMDST_DONE;
  837. } else {
  838. if (!ub_is_completed(&sc->work_done))
  839. break;
  840. del_timer(&sc->work_timer);
  841. ub_scsi_urb_compl(sc, cmd);
  842. }
  843. }
  844. }
  845. static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  846. {
  847. struct urb *urb = &sc->work_urb;
  848. struct bulk_cs_wrap *bcs;
  849. int len;
  850. int rc;
  851. if (atomic_read(&sc->poison)) {
  852. ub_state_done(sc, cmd, -ENODEV);
  853. return;
  854. }
  855. if (cmd->state == UB_CMDST_CLEAR) {
  856. if (urb->status == -EPIPE) {
  857. /*
  858. * STALL while clearning STALL.
  859. * The control pipe clears itself - nothing to do.
  860. */
  861. printk(KERN_NOTICE "%s: stall on control pipe\n",
  862. sc->name);
  863. goto Bad_End;
  864. }
  865. /*
  866. * We ignore the result for the halt clear.
  867. */
  868. /* reset the endpoint toggle */
  869. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  870. usb_pipeout(sc->last_pipe), 0);
  871. ub_state_sense(sc, cmd);
  872. } else if (cmd->state == UB_CMDST_CLR2STS) {
  873. if (urb->status == -EPIPE) {
  874. printk(KERN_NOTICE "%s: stall on control pipe\n",
  875. sc->name);
  876. goto Bad_End;
  877. }
  878. /*
  879. * We ignore the result for the halt clear.
  880. */
  881. /* reset the endpoint toggle */
  882. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  883. usb_pipeout(sc->last_pipe), 0);
  884. ub_state_stat(sc, cmd);
  885. } else if (cmd->state == UB_CMDST_CLRRS) {
  886. if (urb->status == -EPIPE) {
  887. printk(KERN_NOTICE "%s: stall on control pipe\n",
  888. sc->name);
  889. goto Bad_End;
  890. }
  891. /*
  892. * We ignore the result for the halt clear.
  893. */
  894. /* reset the endpoint toggle */
  895. usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
  896. usb_pipeout(sc->last_pipe), 0);
  897. ub_state_stat_counted(sc, cmd);
  898. } else if (cmd->state == UB_CMDST_CMD) {
  899. switch (urb->status) {
  900. case 0:
  901. break;
  902. case -EOVERFLOW:
  903. goto Bad_End;
  904. case -EPIPE:
  905. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  906. if (rc != 0) {
  907. printk(KERN_NOTICE "%s: "
  908. "unable to submit clear (%d)\n",
  909. sc->name, rc);
  910. /*
  911. * This is typically ENOMEM or some other such shit.
  912. * Retrying is pointless. Just do Bad End on it...
  913. */
  914. ub_state_done(sc, cmd, rc);
  915. return;
  916. }
  917. cmd->state = UB_CMDST_CLEAR;
  918. return;
  919. case -ESHUTDOWN: /* unplug */
  920. case -EILSEQ: /* unplug timeout on uhci */
  921. ub_state_done(sc, cmd, -ENODEV);
  922. return;
  923. default:
  924. goto Bad_End;
  925. }
  926. if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
  927. goto Bad_End;
  928. }
  929. if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
  930. ub_state_stat(sc, cmd);
  931. return;
  932. }
  933. // udelay(125); // usb-storage has this
  934. ub_data_start(sc, cmd);
  935. } else if (cmd->state == UB_CMDST_DATA) {
  936. if (urb->status == -EPIPE) {
  937. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  938. if (rc != 0) {
  939. printk(KERN_NOTICE "%s: "
  940. "unable to submit clear (%d)\n",
  941. sc->name, rc);
  942. ub_state_done(sc, cmd, rc);
  943. return;
  944. }
  945. cmd->state = UB_CMDST_CLR2STS;
  946. return;
  947. }
  948. if (urb->status == -EOVERFLOW) {
  949. /*
  950. * A babble? Failure, but we must transfer CSW now.
  951. */
  952. cmd->error = -EOVERFLOW; /* A cheap trick... */
  953. ub_state_stat(sc, cmd);
  954. return;
  955. }
  956. if (cmd->dir == UB_DIR_WRITE) {
  957. /*
  958. * Do not continue writes in case of a failure.
  959. * Doing so would cause sectors to be mixed up,
  960. * which is worse than sectors lost.
  961. *
  962. * We must try to read the CSW, or many devices
  963. * get confused.
  964. */
  965. len = urb->actual_length;
  966. if (urb->status != 0 ||
  967. len != cmd->sgv[cmd->current_sg].length) {
  968. cmd->act_len += len;
  969. cmd->error = -EIO;
  970. ub_state_stat(sc, cmd);
  971. return;
  972. }
  973. } else {
  974. /*
  975. * If an error occurs on read, we record it, and
  976. * continue to fetch data in order to avoid bubble.
  977. *
  978. * As a small shortcut, we stop if we detect that
  979. * a CSW mixed into data.
  980. */
  981. if (urb->status != 0)
  982. cmd->error = -EIO;
  983. len = urb->actual_length;
  984. if (urb->status != 0 ||
  985. len != cmd->sgv[cmd->current_sg].length) {
  986. if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
  987. goto Bad_End;
  988. }
  989. }
  990. cmd->act_len += urb->actual_length;
  991. if (++cmd->current_sg < cmd->nsg) {
  992. ub_data_start(sc, cmd);
  993. return;
  994. }
  995. ub_state_stat(sc, cmd);
  996. } else if (cmd->state == UB_CMDST_STAT) {
  997. if (urb->status == -EPIPE) {
  998. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  999. if (rc != 0) {
  1000. printk(KERN_NOTICE "%s: "
  1001. "unable to submit clear (%d)\n",
  1002. sc->name, rc);
  1003. ub_state_done(sc, cmd, rc);
  1004. return;
  1005. }
  1006. /*
  1007. * Having a stall when getting CSW is an error, so
  1008. * make sure uppper levels are not oblivious to it.
  1009. */
  1010. cmd->error = -EIO; /* A cheap trick... */
  1011. cmd->state = UB_CMDST_CLRRS;
  1012. return;
  1013. }
  1014. /* Catch everything, including -EOVERFLOW and other nasties. */
  1015. if (urb->status != 0)
  1016. goto Bad_End;
  1017. if (urb->actual_length == 0) {
  1018. ub_state_stat_counted(sc, cmd);
  1019. return;
  1020. }
  1021. /*
  1022. * Check the returned Bulk protocol status.
  1023. * The status block has to be validated first.
  1024. */
  1025. bcs = &sc->work_bcs;
  1026. if (sc->signature == cpu_to_le32(0)) {
  1027. /*
  1028. * This is the first reply, so do not perform the check.
  1029. * Instead, remember the signature the device uses
  1030. * for future checks. But do not allow a nul.
  1031. */
  1032. sc->signature = bcs->Signature;
  1033. if (sc->signature == cpu_to_le32(0)) {
  1034. ub_state_stat_counted(sc, cmd);
  1035. return;
  1036. }
  1037. } else {
  1038. if (bcs->Signature != sc->signature) {
  1039. ub_state_stat_counted(sc, cmd);
  1040. return;
  1041. }
  1042. }
  1043. if (bcs->Tag != cmd->tag) {
  1044. /*
  1045. * This usually happens when we disagree with the
  1046. * device's microcode about something. For instance,
  1047. * a few of them throw this after timeouts. They buffer
  1048. * commands and reply at commands we timed out before.
  1049. * Without flushing these replies we loop forever.
  1050. */
  1051. ub_state_stat_counted(sc, cmd);
  1052. return;
  1053. }
  1054. len = le32_to_cpu(bcs->Residue);
  1055. if (len != cmd->len - cmd->act_len) {
  1056. /*
  1057. * It is all right to transfer less, the caller has
  1058. * to check. But it's not all right if the device
  1059. * counts disagree with our counts.
  1060. */
  1061. goto Bad_End;
  1062. }
  1063. switch (bcs->Status) {
  1064. case US_BULK_STAT_OK:
  1065. break;
  1066. case US_BULK_STAT_FAIL:
  1067. ub_state_sense(sc, cmd);
  1068. return;
  1069. case US_BULK_STAT_PHASE:
  1070. goto Bad_End;
  1071. default:
  1072. printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
  1073. sc->name, bcs->Status);
  1074. ub_state_done(sc, cmd, -EINVAL);
  1075. return;
  1076. }
  1077. /* Not zeroing error to preserve a babble indicator */
  1078. if (cmd->error != 0) {
  1079. ub_state_sense(sc, cmd);
  1080. return;
  1081. }
  1082. cmd->state = UB_CMDST_DONE;
  1083. ub_cmdq_pop(sc);
  1084. (*cmd->done)(sc, cmd);
  1085. } else if (cmd->state == UB_CMDST_SENSE) {
  1086. ub_state_done(sc, cmd, -EIO);
  1087. } else {
  1088. printk(KERN_WARNING "%s: "
  1089. "wrong command state %d\n",
  1090. sc->name, cmd->state);
  1091. ub_state_done(sc, cmd, -EINVAL);
  1092. return;
  1093. }
  1094. return;
  1095. Bad_End: /* Little Excel is dead */
  1096. ub_state_done(sc, cmd, -EIO);
  1097. }
  1098. /*
  1099. * Factorization helper for the command state machine:
  1100. * Initiate a data segment transfer.
  1101. */
  1102. static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1103. {
  1104. struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
  1105. int pipe;
  1106. int rc;
  1107. UB_INIT_COMPLETION(sc->work_done);
  1108. if (cmd->dir == UB_DIR_READ)
  1109. pipe = sc->recv_bulk_pipe;
  1110. else
  1111. pipe = sc->send_bulk_pipe;
  1112. sc->last_pipe = pipe;
  1113. usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
  1114. page_address(sg->page) + sg->offset, sg->length,
  1115. ub_urb_complete, sc);
  1116. sc->work_urb.actual_length = 0;
  1117. sc->work_urb.error_count = 0;
  1118. sc->work_urb.status = 0;
  1119. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1120. /* XXX Clear stalls */
  1121. ub_complete(&sc->work_done);
  1122. ub_state_done(sc, cmd, rc);
  1123. return;
  1124. }
  1125. sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
  1126. add_timer(&sc->work_timer);
  1127. cmd->state = UB_CMDST_DATA;
  1128. }
  1129. /*
  1130. * Factorization helper for the command state machine:
  1131. * Finish the command.
  1132. */
  1133. static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
  1134. {
  1135. cmd->error = rc;
  1136. cmd->state = UB_CMDST_DONE;
  1137. ub_cmdq_pop(sc);
  1138. (*cmd->done)(sc, cmd);
  1139. }
  1140. /*
  1141. * Factorization helper for the command state machine:
  1142. * Submit a CSW read.
  1143. */
  1144. static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1145. {
  1146. int rc;
  1147. UB_INIT_COMPLETION(sc->work_done);
  1148. sc->last_pipe = sc->recv_bulk_pipe;
  1149. usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
  1150. &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
  1151. sc->work_urb.actual_length = 0;
  1152. sc->work_urb.error_count = 0;
  1153. sc->work_urb.status = 0;
  1154. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1155. /* XXX Clear stalls */
  1156. ub_complete(&sc->work_done);
  1157. ub_state_done(sc, cmd, rc);
  1158. return -1;
  1159. }
  1160. sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
  1161. add_timer(&sc->work_timer);
  1162. return 0;
  1163. }
  1164. /*
  1165. * Factorization helper for the command state machine:
  1166. * Submit a CSW read and go to STAT state.
  1167. */
  1168. static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1169. {
  1170. if (__ub_state_stat(sc, cmd) != 0)
  1171. return;
  1172. cmd->stat_count = 0;
  1173. cmd->state = UB_CMDST_STAT;
  1174. }
  1175. /*
  1176. * Factorization helper for the command state machine:
  1177. * Submit a CSW read and go to STAT state with counter (along [C] path).
  1178. */
  1179. static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1180. {
  1181. if (++cmd->stat_count >= 4) {
  1182. ub_state_sense(sc, cmd);
  1183. return;
  1184. }
  1185. if (__ub_state_stat(sc, cmd) != 0)
  1186. return;
  1187. cmd->state = UB_CMDST_STAT;
  1188. }
  1189. /*
  1190. * Factorization helper for the command state machine:
  1191. * Submit a REQUEST SENSE and go to SENSE state.
  1192. */
  1193. static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1194. {
  1195. struct ub_scsi_cmd *scmd;
  1196. struct scatterlist *sg;
  1197. int rc;
  1198. if (cmd->cdb[0] == REQUEST_SENSE) {
  1199. rc = -EPIPE;
  1200. goto error;
  1201. }
  1202. scmd = &sc->top_rqs_cmd;
  1203. memset(scmd, 0, sizeof(struct ub_scsi_cmd));
  1204. scmd->cdb[0] = REQUEST_SENSE;
  1205. scmd->cdb[4] = UB_SENSE_SIZE;
  1206. scmd->cdb_len = 6;
  1207. scmd->dir = UB_DIR_READ;
  1208. scmd->state = UB_CMDST_INIT;
  1209. scmd->nsg = 1;
  1210. sg = &scmd->sgv[0];
  1211. sg->page = virt_to_page(sc->top_sense);
  1212. sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
  1213. sg->length = UB_SENSE_SIZE;
  1214. scmd->len = UB_SENSE_SIZE;
  1215. scmd->lun = cmd->lun;
  1216. scmd->done = ub_top_sense_done;
  1217. scmd->back = cmd;
  1218. scmd->tag = sc->tagcnt++;
  1219. cmd->state = UB_CMDST_SENSE;
  1220. ub_cmdq_insert(sc, scmd);
  1221. return;
  1222. error:
  1223. ub_state_done(sc, cmd, rc);
  1224. }
  1225. /*
  1226. * A helper for the command's state machine:
  1227. * Submit a stall clear.
  1228. */
  1229. static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  1230. int stalled_pipe)
  1231. {
  1232. int endp;
  1233. struct usb_ctrlrequest *cr;
  1234. int rc;
  1235. endp = usb_pipeendpoint(stalled_pipe);
  1236. if (usb_pipein (stalled_pipe))
  1237. endp |= USB_DIR_IN;
  1238. cr = &sc->work_cr;
  1239. cr->bRequestType = USB_RECIP_ENDPOINT;
  1240. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1241. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1242. cr->wIndex = cpu_to_le16(endp);
  1243. cr->wLength = cpu_to_le16(0);
  1244. UB_INIT_COMPLETION(sc->work_done);
  1245. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1246. (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
  1247. sc->work_urb.actual_length = 0;
  1248. sc->work_urb.error_count = 0;
  1249. sc->work_urb.status = 0;
  1250. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1251. ub_complete(&sc->work_done);
  1252. return rc;
  1253. }
  1254. sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1255. add_timer(&sc->work_timer);
  1256. return 0;
  1257. }
  1258. /*
  1259. */
  1260. static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
  1261. {
  1262. unsigned char *sense = sc->top_sense;
  1263. struct ub_scsi_cmd *cmd;
  1264. /*
  1265. * Find the command which triggered the unit attention or a check,
  1266. * save the sense into it, and advance its state machine.
  1267. */
  1268. if ((cmd = ub_cmdq_peek(sc)) == NULL) {
  1269. printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
  1270. return;
  1271. }
  1272. if (cmd != scmd->back) {
  1273. printk(KERN_WARNING "%s: "
  1274. "sense done for wrong command 0x%x\n",
  1275. sc->name, cmd->tag);
  1276. return;
  1277. }
  1278. if (cmd->state != UB_CMDST_SENSE) {
  1279. printk(KERN_WARNING "%s: "
  1280. "sense done with bad cmd state %d\n",
  1281. sc->name, cmd->state);
  1282. return;
  1283. }
  1284. /*
  1285. * Ignoring scmd->act_len, because the buffer was pre-zeroed.
  1286. */
  1287. cmd->key = sense[2] & 0x0F;
  1288. cmd->asc = sense[12];
  1289. cmd->ascq = sense[13];
  1290. ub_scsi_urb_compl(sc, cmd);
  1291. }
  1292. /*
  1293. * Reset management
  1294. * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
  1295. * XXX Make usb_sync_reset asynchronous.
  1296. */
  1297. static void ub_reset_enter(struct ub_dev *sc, int try)
  1298. {
  1299. if (sc->reset) {
  1300. /* This happens often on multi-LUN devices. */
  1301. return;
  1302. }
  1303. sc->reset = try + 1;
  1304. #if 0 /* Not needed because the disconnect waits for us. */
  1305. unsigned long flags;
  1306. spin_lock_irqsave(&ub_lock, flags);
  1307. sc->openc++;
  1308. spin_unlock_irqrestore(&ub_lock, flags);
  1309. #endif
  1310. #if 0 /* We let them stop themselves. */
  1311. struct list_head *p;
  1312. struct ub_lun *lun;
  1313. list_for_each(p, &sc->luns) {
  1314. lun = list_entry(p, struct ub_lun, link);
  1315. blk_stop_queue(lun->disk->queue);
  1316. }
  1317. #endif
  1318. schedule_work(&sc->reset_work);
  1319. }
  1320. static void ub_reset_task(void *arg)
  1321. {
  1322. struct ub_dev *sc = arg;
  1323. unsigned long flags;
  1324. struct list_head *p;
  1325. struct ub_lun *lun;
  1326. int lkr, rc;
  1327. if (!sc->reset) {
  1328. printk(KERN_WARNING "%s: Running reset unrequested\n",
  1329. sc->name);
  1330. return;
  1331. }
  1332. if (atomic_read(&sc->poison)) {
  1333. ;
  1334. } else if ((sc->reset & 1) == 0) {
  1335. ub_sync_reset(sc);
  1336. msleep(700); /* usb-storage sleeps 6s (!) */
  1337. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  1338. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  1339. } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
  1340. ;
  1341. } else {
  1342. if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
  1343. printk(KERN_NOTICE
  1344. "%s: usb_lock_device_for_reset failed (%d)\n",
  1345. sc->name, lkr);
  1346. } else {
  1347. rc = usb_reset_device(sc->dev);
  1348. if (rc < 0) {
  1349. printk(KERN_NOTICE "%s: "
  1350. "usb_lock_device_for_reset failed (%d)\n",
  1351. sc->name, rc);
  1352. }
  1353. if (lkr)
  1354. usb_unlock_device(sc->dev);
  1355. }
  1356. }
  1357. /*
  1358. * In theory, no commands can be running while reset is active,
  1359. * so nobody can ask for another reset, and so we do not need any
  1360. * queues of resets or anything. We do need a spinlock though,
  1361. * to interact with block layer.
  1362. */
  1363. spin_lock_irqsave(sc->lock, flags);
  1364. sc->reset = 0;
  1365. tasklet_schedule(&sc->tasklet);
  1366. list_for_each(p, &sc->luns) {
  1367. lun = list_entry(p, struct ub_lun, link);
  1368. blk_start_queue(lun->disk->queue);
  1369. }
  1370. wake_up(&sc->reset_wait);
  1371. spin_unlock_irqrestore(sc->lock, flags);
  1372. }
  1373. /*
  1374. * This is called from a process context.
  1375. */
  1376. static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
  1377. {
  1378. lun->readonly = 0; /* XXX Query this from the device */
  1379. lun->capacity.nsec = 0;
  1380. lun->capacity.bsize = 512;
  1381. lun->capacity.bshift = 0;
  1382. if (ub_sync_tur(sc, lun) != 0)
  1383. return; /* Not ready */
  1384. lun->changed = 0;
  1385. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1386. /*
  1387. * The retry here means something is wrong, either with the
  1388. * device, with the transport, or with our code.
  1389. * We keep this because sd.c has retries for capacity.
  1390. */
  1391. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1392. lun->capacity.nsec = 0;
  1393. lun->capacity.bsize = 512;
  1394. lun->capacity.bshift = 0;
  1395. }
  1396. }
  1397. }
  1398. /*
  1399. * The open funcion.
  1400. * This is mostly needed to keep refcounting, but also to support
  1401. * media checks on removable media drives.
  1402. */
  1403. static int ub_bd_open(struct inode *inode, struct file *filp)
  1404. {
  1405. struct gendisk *disk = inode->i_bdev->bd_disk;
  1406. struct ub_lun *lun = disk->private_data;
  1407. struct ub_dev *sc = lun->udev;
  1408. unsigned long flags;
  1409. int rc;
  1410. spin_lock_irqsave(&ub_lock, flags);
  1411. if (atomic_read(&sc->poison)) {
  1412. spin_unlock_irqrestore(&ub_lock, flags);
  1413. return -ENXIO;
  1414. }
  1415. sc->openc++;
  1416. spin_unlock_irqrestore(&ub_lock, flags);
  1417. if (lun->removable || lun->readonly)
  1418. check_disk_change(inode->i_bdev);
  1419. /*
  1420. * The sd.c considers ->media_present and ->changed not equivalent,
  1421. * under some pretty murky conditions (a failure of READ CAPACITY).
  1422. * We may need it one day.
  1423. */
  1424. if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
  1425. rc = -ENOMEDIUM;
  1426. goto err_open;
  1427. }
  1428. if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
  1429. rc = -EROFS;
  1430. goto err_open;
  1431. }
  1432. return 0;
  1433. err_open:
  1434. ub_put(sc);
  1435. return rc;
  1436. }
  1437. /*
  1438. */
  1439. static int ub_bd_release(struct inode *inode, struct file *filp)
  1440. {
  1441. struct gendisk *disk = inode->i_bdev->bd_disk;
  1442. struct ub_lun *lun = disk->private_data;
  1443. struct ub_dev *sc = lun->udev;
  1444. ub_put(sc);
  1445. return 0;
  1446. }
  1447. /*
  1448. * The ioctl interface.
  1449. */
  1450. static int ub_bd_ioctl(struct inode *inode, struct file *filp,
  1451. unsigned int cmd, unsigned long arg)
  1452. {
  1453. struct gendisk *disk = inode->i_bdev->bd_disk;
  1454. void __user *usermem = (void __user *) arg;
  1455. return scsi_cmd_ioctl(filp, disk, cmd, usermem);
  1456. }
  1457. /*
  1458. * This is called once a new disk was seen by the block layer or by ub_probe().
  1459. * The main onjective here is to discover the features of the media such as
  1460. * the capacity, read-only status, etc. USB storage generally does not
  1461. * need to be spun up, but if we needed it, this would be the place.
  1462. *
  1463. * This call can sleep.
  1464. *
  1465. * The return code is not used.
  1466. */
  1467. static int ub_bd_revalidate(struct gendisk *disk)
  1468. {
  1469. struct ub_lun *lun = disk->private_data;
  1470. ub_revalidate(lun->udev, lun);
  1471. /* XXX Support sector size switching like in sr.c */
  1472. blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
  1473. set_capacity(disk, lun->capacity.nsec);
  1474. // set_disk_ro(sdkp->disk, lun->readonly);
  1475. return 0;
  1476. }
  1477. /*
  1478. * The check is called by the block layer to verify if the media
  1479. * is still available. It is supposed to be harmless, lightweight and
  1480. * non-intrusive in case the media was not changed.
  1481. *
  1482. * This call can sleep.
  1483. *
  1484. * The return code is bool!
  1485. */
  1486. static int ub_bd_media_changed(struct gendisk *disk)
  1487. {
  1488. struct ub_lun *lun = disk->private_data;
  1489. if (!lun->removable)
  1490. return 0;
  1491. /*
  1492. * We clean checks always after every command, so this is not
  1493. * as dangerous as it looks. If the TEST_UNIT_READY fails here,
  1494. * the device is actually not ready with operator or software
  1495. * intervention required. One dangerous item might be a drive which
  1496. * spins itself down, and come the time to write dirty pages, this
  1497. * will fail, then block layer discards the data. Since we never
  1498. * spin drives up, such devices simply cannot be used with ub anyway.
  1499. */
  1500. if (ub_sync_tur(lun->udev, lun) != 0) {
  1501. lun->changed = 1;
  1502. return 1;
  1503. }
  1504. return lun->changed;
  1505. }
  1506. static struct block_device_operations ub_bd_fops = {
  1507. .owner = THIS_MODULE,
  1508. .open = ub_bd_open,
  1509. .release = ub_bd_release,
  1510. .ioctl = ub_bd_ioctl,
  1511. .media_changed = ub_bd_media_changed,
  1512. .revalidate_disk = ub_bd_revalidate,
  1513. };
  1514. /*
  1515. * Common ->done routine for commands executed synchronously.
  1516. */
  1517. static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1518. {
  1519. struct completion *cop = cmd->back;
  1520. complete(cop);
  1521. }
  1522. /*
  1523. * Test if the device has a check condition on it, synchronously.
  1524. */
  1525. static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
  1526. {
  1527. struct ub_scsi_cmd *cmd;
  1528. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
  1529. unsigned long flags;
  1530. struct completion compl;
  1531. int rc;
  1532. init_completion(&compl);
  1533. rc = -ENOMEM;
  1534. if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1535. goto err_alloc;
  1536. cmd->cdb[0] = TEST_UNIT_READY;
  1537. cmd->cdb_len = 6;
  1538. cmd->dir = UB_DIR_NONE;
  1539. cmd->state = UB_CMDST_INIT;
  1540. cmd->lun = lun; /* This may be NULL, but that's ok */
  1541. cmd->done = ub_probe_done;
  1542. cmd->back = &compl;
  1543. spin_lock_irqsave(sc->lock, flags);
  1544. cmd->tag = sc->tagcnt++;
  1545. rc = ub_submit_scsi(sc, cmd);
  1546. spin_unlock_irqrestore(sc->lock, flags);
  1547. if (rc != 0)
  1548. goto err_submit;
  1549. wait_for_completion(&compl);
  1550. rc = cmd->error;
  1551. if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
  1552. rc = cmd->key;
  1553. err_submit:
  1554. kfree(cmd);
  1555. err_alloc:
  1556. return rc;
  1557. }
  1558. /*
  1559. * Read the SCSI capacity synchronously (for probing).
  1560. */
  1561. static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
  1562. struct ub_capacity *ret)
  1563. {
  1564. struct ub_scsi_cmd *cmd;
  1565. struct scatterlist *sg;
  1566. char *p;
  1567. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
  1568. unsigned long flags;
  1569. unsigned int bsize, shift;
  1570. unsigned long nsec;
  1571. struct completion compl;
  1572. int rc;
  1573. init_completion(&compl);
  1574. rc = -ENOMEM;
  1575. if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1576. goto err_alloc;
  1577. p = (char *)cmd + sizeof(struct ub_scsi_cmd);
  1578. cmd->cdb[0] = 0x25;
  1579. cmd->cdb_len = 10;
  1580. cmd->dir = UB_DIR_READ;
  1581. cmd->state = UB_CMDST_INIT;
  1582. cmd->nsg = 1;
  1583. sg = &cmd->sgv[0];
  1584. sg->page = virt_to_page(p);
  1585. sg->offset = (unsigned long)p & (PAGE_SIZE-1);
  1586. sg->length = 8;
  1587. cmd->len = 8;
  1588. cmd->lun = lun;
  1589. cmd->done = ub_probe_done;
  1590. cmd->back = &compl;
  1591. spin_lock_irqsave(sc->lock, flags);
  1592. cmd->tag = sc->tagcnt++;
  1593. rc = ub_submit_scsi(sc, cmd);
  1594. spin_unlock_irqrestore(sc->lock, flags);
  1595. if (rc != 0)
  1596. goto err_submit;
  1597. wait_for_completion(&compl);
  1598. if (cmd->error != 0) {
  1599. rc = -EIO;
  1600. goto err_read;
  1601. }
  1602. if (cmd->act_len != 8) {
  1603. rc = -EIO;
  1604. goto err_read;
  1605. }
  1606. /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
  1607. nsec = be32_to_cpu(*(__be32 *)p) + 1;
  1608. bsize = be32_to_cpu(*(__be32 *)(p + 4));
  1609. switch (bsize) {
  1610. case 512: shift = 0; break;
  1611. case 1024: shift = 1; break;
  1612. case 2048: shift = 2; break;
  1613. case 4096: shift = 3; break;
  1614. default:
  1615. rc = -EDOM;
  1616. goto err_inv_bsize;
  1617. }
  1618. ret->bsize = bsize;
  1619. ret->bshift = shift;
  1620. ret->nsec = nsec << shift;
  1621. rc = 0;
  1622. err_inv_bsize:
  1623. err_read:
  1624. err_submit:
  1625. kfree(cmd);
  1626. err_alloc:
  1627. return rc;
  1628. }
  1629. /*
  1630. */
  1631. static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
  1632. {
  1633. struct completion *cop = urb->context;
  1634. complete(cop);
  1635. }
  1636. static void ub_probe_timeout(unsigned long arg)
  1637. {
  1638. struct completion *cop = (struct completion *) arg;
  1639. complete(cop);
  1640. }
  1641. /*
  1642. * Reset with a Bulk reset.
  1643. */
  1644. static int ub_sync_reset(struct ub_dev *sc)
  1645. {
  1646. int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
  1647. struct usb_ctrlrequest *cr;
  1648. struct completion compl;
  1649. struct timer_list timer;
  1650. int rc;
  1651. init_completion(&compl);
  1652. cr = &sc->work_cr;
  1653. cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1654. cr->bRequest = US_BULK_RESET_REQUEST;
  1655. cr->wValue = cpu_to_le16(0);
  1656. cr->wIndex = cpu_to_le16(ifnum);
  1657. cr->wLength = cpu_to_le16(0);
  1658. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1659. (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
  1660. sc->work_urb.actual_length = 0;
  1661. sc->work_urb.error_count = 0;
  1662. sc->work_urb.status = 0;
  1663. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1664. printk(KERN_WARNING
  1665. "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
  1666. return rc;
  1667. }
  1668. init_timer(&timer);
  1669. timer.function = ub_probe_timeout;
  1670. timer.data = (unsigned long) &compl;
  1671. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1672. add_timer(&timer);
  1673. wait_for_completion(&compl);
  1674. del_timer_sync(&timer);
  1675. usb_kill_urb(&sc->work_urb);
  1676. return sc->work_urb.status;
  1677. }
  1678. /*
  1679. * Get number of LUNs by the way of Bulk GetMaxLUN command.
  1680. */
  1681. static int ub_sync_getmaxlun(struct ub_dev *sc)
  1682. {
  1683. int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
  1684. unsigned char *p;
  1685. enum { ALLOC_SIZE = 1 };
  1686. struct usb_ctrlrequest *cr;
  1687. struct completion compl;
  1688. struct timer_list timer;
  1689. int nluns;
  1690. int rc;
  1691. init_completion(&compl);
  1692. rc = -ENOMEM;
  1693. if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1694. goto err_alloc;
  1695. *p = 55;
  1696. cr = &sc->work_cr;
  1697. cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1698. cr->bRequest = US_BULK_GET_MAX_LUN;
  1699. cr->wValue = cpu_to_le16(0);
  1700. cr->wIndex = cpu_to_le16(ifnum);
  1701. cr->wLength = cpu_to_le16(1);
  1702. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
  1703. (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
  1704. sc->work_urb.actual_length = 0;
  1705. sc->work_urb.error_count = 0;
  1706. sc->work_urb.status = 0;
  1707. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
  1708. goto err_submit;
  1709. init_timer(&timer);
  1710. timer.function = ub_probe_timeout;
  1711. timer.data = (unsigned long) &compl;
  1712. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1713. add_timer(&timer);
  1714. wait_for_completion(&compl);
  1715. del_timer_sync(&timer);
  1716. usb_kill_urb(&sc->work_urb);
  1717. if ((rc = sc->work_urb.status) < 0)
  1718. goto err_io;
  1719. if (sc->work_urb.actual_length != 1) {
  1720. nluns = 0;
  1721. } else {
  1722. if ((nluns = *p) == 55) {
  1723. nluns = 0;
  1724. } else {
  1725. /* GetMaxLUN returns the maximum LUN number */
  1726. nluns += 1;
  1727. if (nluns > UB_MAX_LUNS)
  1728. nluns = UB_MAX_LUNS;
  1729. }
  1730. }
  1731. kfree(p);
  1732. return nluns;
  1733. err_io:
  1734. err_submit:
  1735. kfree(p);
  1736. err_alloc:
  1737. return rc;
  1738. }
  1739. /*
  1740. * Clear initial stalls.
  1741. */
  1742. static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
  1743. {
  1744. int endp;
  1745. struct usb_ctrlrequest *cr;
  1746. struct completion compl;
  1747. struct timer_list timer;
  1748. int rc;
  1749. init_completion(&compl);
  1750. endp = usb_pipeendpoint(stalled_pipe);
  1751. if (usb_pipein (stalled_pipe))
  1752. endp |= USB_DIR_IN;
  1753. cr = &sc->work_cr;
  1754. cr->bRequestType = USB_RECIP_ENDPOINT;
  1755. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1756. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1757. cr->wIndex = cpu_to_le16(endp);
  1758. cr->wLength = cpu_to_le16(0);
  1759. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1760. (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
  1761. sc->work_urb.actual_length = 0;
  1762. sc->work_urb.error_count = 0;
  1763. sc->work_urb.status = 0;
  1764. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1765. printk(KERN_WARNING
  1766. "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
  1767. return rc;
  1768. }
  1769. init_timer(&timer);
  1770. timer.function = ub_probe_timeout;
  1771. timer.data = (unsigned long) &compl;
  1772. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1773. add_timer(&timer);
  1774. wait_for_completion(&compl);
  1775. del_timer_sync(&timer);
  1776. usb_kill_urb(&sc->work_urb);
  1777. /* reset the endpoint toggle */
  1778. usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
  1779. return 0;
  1780. }
  1781. /*
  1782. * Get the pipe settings.
  1783. */
  1784. static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
  1785. struct usb_interface *intf)
  1786. {
  1787. struct usb_host_interface *altsetting = intf->cur_altsetting;
  1788. struct usb_endpoint_descriptor *ep_in = NULL;
  1789. struct usb_endpoint_descriptor *ep_out = NULL;
  1790. struct usb_endpoint_descriptor *ep;
  1791. int i;
  1792. /*
  1793. * Find the endpoints we need.
  1794. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1795. * We will ignore any others.
  1796. */
  1797. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  1798. ep = &altsetting->endpoint[i].desc;
  1799. /* Is it a BULK endpoint? */
  1800. if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1801. == USB_ENDPOINT_XFER_BULK) {
  1802. /* BULK in or out? */
  1803. if (ep->bEndpointAddress & USB_DIR_IN)
  1804. ep_in = ep;
  1805. else
  1806. ep_out = ep;
  1807. }
  1808. }
  1809. if (ep_in == NULL || ep_out == NULL) {
  1810. printk(KERN_NOTICE "%s: failed endpoint check\n",
  1811. sc->name);
  1812. return -ENODEV;
  1813. }
  1814. /* Calculate and store the pipe values */
  1815. sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
  1816. sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
  1817. sc->send_bulk_pipe = usb_sndbulkpipe(dev,
  1818. ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1819. sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
  1820. ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  1821. return 0;
  1822. }
  1823. /*
  1824. * Probing is done in the process context, which allows us to cheat
  1825. * and not to build a state machine for the discovery.
  1826. */
  1827. static int ub_probe(struct usb_interface *intf,
  1828. const struct usb_device_id *dev_id)
  1829. {
  1830. struct ub_dev *sc;
  1831. int nluns;
  1832. int rc;
  1833. int i;
  1834. if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
  1835. return -ENXIO;
  1836. rc = -ENOMEM;
  1837. if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
  1838. goto err_core;
  1839. sc->lock = ub_next_lock();
  1840. INIT_LIST_HEAD(&sc->luns);
  1841. usb_init_urb(&sc->work_urb);
  1842. tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
  1843. atomic_set(&sc->poison, 0);
  1844. INIT_WORK(&sc->reset_work, ub_reset_task, sc);
  1845. init_waitqueue_head(&sc->reset_wait);
  1846. init_timer(&sc->work_timer);
  1847. sc->work_timer.data = (unsigned long) sc;
  1848. sc->work_timer.function = ub_urb_timeout;
  1849. ub_init_completion(&sc->work_done);
  1850. sc->work_done.done = 1; /* A little yuk, but oh well... */
  1851. sc->dev = interface_to_usbdev(intf);
  1852. sc->intf = intf;
  1853. // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1854. usb_set_intfdata(intf, sc);
  1855. usb_get_dev(sc->dev);
  1856. /*
  1857. * Since we give the interface struct to the block level through
  1858. * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
  1859. * oopses on close after a disconnect (kernels 2.6.16 and up).
  1860. */
  1861. usb_get_intf(sc->intf);
  1862. snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
  1863. sc->dev->bus->busnum, sc->dev->devnum);
  1864. /* XXX Verify that we can handle the device (from descriptors) */
  1865. if (ub_get_pipes(sc, sc->dev, intf) != 0)
  1866. goto err_dev_desc;
  1867. /*
  1868. * At this point, all USB initialization is done, do upper layer.
  1869. * We really hate halfway initialized structures, so from the
  1870. * invariants perspective, this ub_dev is fully constructed at
  1871. * this point.
  1872. */
  1873. /*
  1874. * This is needed to clear toggles. It is a problem only if we do
  1875. * `rmmod ub && modprobe ub` without disconnects, but we like that.
  1876. */
  1877. #if 0 /* iPod Mini fails if we do this (big white iPod works) */
  1878. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  1879. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  1880. #endif
  1881. /*
  1882. * The way this is used by the startup code is a little specific.
  1883. * A SCSI check causes a USB stall. Our common case code sees it
  1884. * and clears the check, after which the device is ready for use.
  1885. * But if a check was not present, any command other than
  1886. * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
  1887. *
  1888. * If we neglect to clear the SCSI check, the first real command fails
  1889. * (which is the capacity readout). We clear that and retry, but why
  1890. * causing spurious retries for no reason.
  1891. *
  1892. * Revalidation may start with its own TEST_UNIT_READY, but that one
  1893. * has to succeed, so we clear checks with an additional one here.
  1894. * In any case it's not our business how revaliadation is implemented.
  1895. */
  1896. for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
  1897. if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
  1898. if (rc != 0x6) break;
  1899. msleep(10);
  1900. }
  1901. nluns = 1;
  1902. for (i = 0; i < 3; i++) {
  1903. if ((rc = ub_sync_getmaxlun(sc)) < 0)
  1904. break;
  1905. if (rc != 0) {
  1906. nluns = rc;
  1907. break;
  1908. }
  1909. msleep(100);
  1910. }
  1911. for (i = 0; i < nluns; i++) {
  1912. ub_probe_lun(sc, i);
  1913. }
  1914. return 0;
  1915. err_dev_desc:
  1916. usb_set_intfdata(intf, NULL);
  1917. usb_put_intf(sc->intf);
  1918. usb_put_dev(sc->dev);
  1919. kfree(sc);
  1920. err_core:
  1921. return rc;
  1922. }
  1923. static int ub_probe_lun(struct ub_dev *sc, int lnum)
  1924. {
  1925. struct ub_lun *lun;
  1926. request_queue_t *q;
  1927. struct gendisk *disk;
  1928. int rc;
  1929. rc = -ENOMEM;
  1930. if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
  1931. goto err_alloc;
  1932. lun->num = lnum;
  1933. rc = -ENOSR;
  1934. if ((lun->id = ub_id_get()) == -1)
  1935. goto err_id;
  1936. lun->udev = sc;
  1937. snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
  1938. lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
  1939. lun->removable = 1; /* XXX Query this from the device */
  1940. lun->changed = 1; /* ub_revalidate clears only */
  1941. ub_revalidate(sc, lun);
  1942. rc = -ENOMEM;
  1943. if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
  1944. goto err_diskalloc;
  1945. sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
  1946. sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
  1947. disk->major = UB_MAJOR;
  1948. disk->first_minor = lun->id * UB_PARTS_PER_LUN;
  1949. disk->fops = &ub_bd_fops;
  1950. disk->private_data = lun;
  1951. disk->driverfs_dev = &sc->intf->dev;
  1952. rc = -ENOMEM;
  1953. if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
  1954. goto err_blkqinit;
  1955. disk->queue = q;
  1956. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  1957. blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
  1958. blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
  1959. blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
  1960. blk_queue_max_sectors(q, UB_MAX_SECTORS);
  1961. blk_queue_hardsect_size(q, lun->capacity.bsize);
  1962. lun->disk = disk;
  1963. q->queuedata = lun;
  1964. list_add(&lun->link, &sc->luns);
  1965. set_capacity(disk, lun->capacity.nsec);
  1966. if (lun->removable)
  1967. disk->flags |= GENHD_FL_REMOVABLE;
  1968. add_disk(disk);
  1969. return 0;
  1970. err_blkqinit:
  1971. put_disk(disk);
  1972. err_diskalloc:
  1973. ub_id_put(lun->id);
  1974. err_id:
  1975. kfree(lun);
  1976. err_alloc:
  1977. return rc;
  1978. }
  1979. static void ub_disconnect(struct usb_interface *intf)
  1980. {
  1981. struct ub_dev *sc = usb_get_intfdata(intf);
  1982. struct list_head *p;
  1983. struct ub_lun *lun;
  1984. unsigned long flags;
  1985. /*
  1986. * Prevent ub_bd_release from pulling the rug from under us.
  1987. * XXX This is starting to look like a kref.
  1988. * XXX Why not to take this ref at probe time?
  1989. */
  1990. spin_lock_irqsave(&ub_lock, flags);
  1991. sc->openc++;
  1992. spin_unlock_irqrestore(&ub_lock, flags);
  1993. /*
  1994. * Fence stall clearnings, operations triggered by unlinkings and so on.
  1995. * We do not attempt to unlink any URBs, because we do not trust the
  1996. * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
  1997. */
  1998. atomic_set(&sc->poison, 1);
  1999. /*
  2000. * Wait for reset to end, if any.
  2001. */
  2002. wait_event(sc->reset_wait, !sc->reset);
  2003. /*
  2004. * Blow away queued commands.
  2005. *
  2006. * Actually, this never works, because before we get here
  2007. * the HCD terminates outstanding URB(s). It causes our
  2008. * SCSI command queue to advance, commands fail to submit,
  2009. * and the whole queue drains. So, we just use this code to
  2010. * print warnings.
  2011. */
  2012. spin_lock_irqsave(sc->lock, flags);
  2013. {
  2014. struct ub_scsi_cmd *cmd;
  2015. int cnt = 0;
  2016. while ((cmd = ub_cmdq_peek(sc)) != NULL) {
  2017. cmd->error = -ENOTCONN;
  2018. cmd->state = UB_CMDST_DONE;
  2019. ub_cmdq_pop(sc);
  2020. (*cmd->done)(sc, cmd);
  2021. cnt++;
  2022. }
  2023. if (cnt != 0) {
  2024. printk(KERN_WARNING "%s: "
  2025. "%d was queued after shutdown\n", sc->name, cnt);
  2026. }
  2027. }
  2028. spin_unlock_irqrestore(sc->lock, flags);
  2029. /*
  2030. * Unregister the upper layer.
  2031. */
  2032. list_for_each (p, &sc->luns) {
  2033. lun = list_entry(p, struct ub_lun, link);
  2034. del_gendisk(lun->disk);
  2035. /*
  2036. * I wish I could do:
  2037. * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
  2038. * As it is, we rely on our internal poisoning and let
  2039. * the upper levels to spin furiously failing all the I/O.
  2040. */
  2041. }
  2042. /*
  2043. * Testing for -EINPROGRESS is always a bug, so we are bending
  2044. * the rules a little.
  2045. */
  2046. spin_lock_irqsave(sc->lock, flags);
  2047. if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
  2048. printk(KERN_WARNING "%s: "
  2049. "URB is active after disconnect\n", sc->name);
  2050. }
  2051. spin_unlock_irqrestore(sc->lock, flags);
  2052. /*
  2053. * There is virtually no chance that other CPU runs times so long
  2054. * after ub_urb_complete should have called del_timer, but only if HCD
  2055. * didn't forget to deliver a callback on unlink.
  2056. */
  2057. del_timer_sync(&sc->work_timer);
  2058. /*
  2059. * At this point there must be no commands coming from anyone
  2060. * and no URBs left in transit.
  2061. */
  2062. ub_put(sc);
  2063. }
  2064. static struct usb_driver ub_driver = {
  2065. .name = "ub",
  2066. .probe = ub_probe,
  2067. .disconnect = ub_disconnect,
  2068. .id_table = ub_usb_ids,
  2069. };
  2070. static int __init ub_init(void)
  2071. {
  2072. int rc;
  2073. int i;
  2074. for (i = 0; i < UB_QLOCK_NUM; i++)
  2075. spin_lock_init(&ub_qlockv[i]);
  2076. if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
  2077. goto err_regblkdev;
  2078. if ((rc = usb_register(&ub_driver)) != 0)
  2079. goto err_register;
  2080. usb_usual_set_present(USB_US_TYPE_UB);
  2081. return 0;
  2082. err_register:
  2083. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2084. err_regblkdev:
  2085. return rc;
  2086. }
  2087. static void __exit ub_exit(void)
  2088. {
  2089. usb_deregister(&ub_driver);
  2090. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2091. usb_usual_clear_present(USB_US_TYPE_UB);
  2092. }
  2093. module_init(ub_init);
  2094. module_exit(ub_exit);
  2095. MODULE_LICENSE("GPL");