ub.c 62 KB

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