ub.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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 usb_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 endp;
  883. int len;
  884. int rc;
  885. if (atomic_read(&sc->poison)) {
  886. ub_state_done(sc, cmd, -ENODEV);
  887. return;
  888. }
  889. endp = usb_pipeendpoint(sc->last_pipe);
  890. if (usb_pipein(sc->last_pipe))
  891. endp |= USB_DIR_IN;
  892. if (cmd->state == UB_CMDST_CLEAR) {
  893. if (urb->status == -EPIPE) {
  894. /*
  895. * STALL while clearning STALL.
  896. * The control pipe clears itself - nothing to do.
  897. */
  898. printk(KERN_NOTICE "%s: stall on control pipe\n",
  899. sc->name);
  900. goto Bad_End;
  901. }
  902. /*
  903. * We ignore the result for the halt clear.
  904. */
  905. usb_reset_endpoint(sc->dev, endp);
  906. ub_state_sense(sc, cmd);
  907. } else if (cmd->state == UB_CMDST_CLR2STS) {
  908. if (urb->status == -EPIPE) {
  909. printk(KERN_NOTICE "%s: stall on control pipe\n",
  910. sc->name);
  911. goto Bad_End;
  912. }
  913. /*
  914. * We ignore the result for the halt clear.
  915. */
  916. usb_reset_endpoint(sc->dev, endp);
  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. usb_reset_endpoint(sc->dev, endp);
  928. ub_state_stat_counted(sc, cmd);
  929. } else if (cmd->state == UB_CMDST_CMD) {
  930. switch (urb->status) {
  931. case 0:
  932. break;
  933. case -EOVERFLOW:
  934. goto Bad_End;
  935. case -EPIPE:
  936. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  937. if (rc != 0) {
  938. printk(KERN_NOTICE "%s: "
  939. "unable to submit clear (%d)\n",
  940. sc->name, rc);
  941. /*
  942. * This is typically ENOMEM or some other such shit.
  943. * Retrying is pointless. Just do Bad End on it...
  944. */
  945. ub_state_done(sc, cmd, rc);
  946. return;
  947. }
  948. cmd->state = UB_CMDST_CLEAR;
  949. return;
  950. case -ESHUTDOWN: /* unplug */
  951. case -EILSEQ: /* unplug timeout on uhci */
  952. ub_state_done(sc, cmd, -ENODEV);
  953. return;
  954. default:
  955. goto Bad_End;
  956. }
  957. if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
  958. goto Bad_End;
  959. }
  960. if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
  961. ub_state_stat(sc, cmd);
  962. return;
  963. }
  964. // udelay(125); // usb-storage has this
  965. ub_data_start(sc, cmd);
  966. } else if (cmd->state == UB_CMDST_DATA) {
  967. if (urb->status == -EPIPE) {
  968. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  969. if (rc != 0) {
  970. printk(KERN_NOTICE "%s: "
  971. "unable to submit clear (%d)\n",
  972. sc->name, rc);
  973. ub_state_done(sc, cmd, rc);
  974. return;
  975. }
  976. cmd->state = UB_CMDST_CLR2STS;
  977. return;
  978. }
  979. if (urb->status == -EOVERFLOW) {
  980. /*
  981. * A babble? Failure, but we must transfer CSW now.
  982. */
  983. cmd->error = -EOVERFLOW; /* A cheap trick... */
  984. ub_state_stat(sc, cmd);
  985. return;
  986. }
  987. if (cmd->dir == UB_DIR_WRITE) {
  988. /*
  989. * Do not continue writes in case of a failure.
  990. * Doing so would cause sectors to be mixed up,
  991. * which is worse than sectors lost.
  992. *
  993. * We must try to read the CSW, or many devices
  994. * get confused.
  995. */
  996. len = urb->actual_length;
  997. if (urb->status != 0 ||
  998. len != cmd->sgv[cmd->current_sg].length) {
  999. cmd->act_len += len;
  1000. cmd->error = -EIO;
  1001. ub_state_stat(sc, cmd);
  1002. return;
  1003. }
  1004. } else {
  1005. /*
  1006. * If an error occurs on read, we record it, and
  1007. * continue to fetch data in order to avoid bubble.
  1008. *
  1009. * As a small shortcut, we stop if we detect that
  1010. * a CSW mixed into data.
  1011. */
  1012. if (urb->status != 0)
  1013. cmd->error = -EIO;
  1014. len = urb->actual_length;
  1015. if (urb->status != 0 ||
  1016. len != cmd->sgv[cmd->current_sg].length) {
  1017. if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
  1018. goto Bad_End;
  1019. }
  1020. }
  1021. cmd->act_len += urb->actual_length;
  1022. if (++cmd->current_sg < cmd->nsg) {
  1023. ub_data_start(sc, cmd);
  1024. return;
  1025. }
  1026. ub_state_stat(sc, cmd);
  1027. } else if (cmd->state == UB_CMDST_STAT) {
  1028. if (urb->status == -EPIPE) {
  1029. rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
  1030. if (rc != 0) {
  1031. printk(KERN_NOTICE "%s: "
  1032. "unable to submit clear (%d)\n",
  1033. sc->name, rc);
  1034. ub_state_done(sc, cmd, rc);
  1035. return;
  1036. }
  1037. /*
  1038. * Having a stall when getting CSW is an error, so
  1039. * make sure uppper levels are not oblivious to it.
  1040. */
  1041. cmd->error = -EIO; /* A cheap trick... */
  1042. cmd->state = UB_CMDST_CLRRS;
  1043. return;
  1044. }
  1045. /* Catch everything, including -EOVERFLOW and other nasties. */
  1046. if (urb->status != 0)
  1047. goto Bad_End;
  1048. if (urb->actual_length == 0) {
  1049. ub_state_stat_counted(sc, cmd);
  1050. return;
  1051. }
  1052. /*
  1053. * Check the returned Bulk protocol status.
  1054. * The status block has to be validated first.
  1055. */
  1056. bcs = &sc->work_bcs;
  1057. if (sc->signature == cpu_to_le32(0)) {
  1058. /*
  1059. * This is the first reply, so do not perform the check.
  1060. * Instead, remember the signature the device uses
  1061. * for future checks. But do not allow a nul.
  1062. */
  1063. sc->signature = bcs->Signature;
  1064. if (sc->signature == cpu_to_le32(0)) {
  1065. ub_state_stat_counted(sc, cmd);
  1066. return;
  1067. }
  1068. } else {
  1069. if (bcs->Signature != sc->signature) {
  1070. ub_state_stat_counted(sc, cmd);
  1071. return;
  1072. }
  1073. }
  1074. if (bcs->Tag != cmd->tag) {
  1075. /*
  1076. * This usually happens when we disagree with the
  1077. * device's microcode about something. For instance,
  1078. * a few of them throw this after timeouts. They buffer
  1079. * commands and reply at commands we timed out before.
  1080. * Without flushing these replies we loop forever.
  1081. */
  1082. ub_state_stat_counted(sc, cmd);
  1083. return;
  1084. }
  1085. if (!sc->bad_resid) {
  1086. len = le32_to_cpu(bcs->Residue);
  1087. if (len != cmd->len - cmd->act_len) {
  1088. /*
  1089. * Only start ignoring if this cmd ended well.
  1090. */
  1091. if (cmd->len == cmd->act_len) {
  1092. printk(KERN_NOTICE "%s: "
  1093. "bad residual %d of %d, ignoring\n",
  1094. sc->name, len, cmd->len);
  1095. sc->bad_resid = 1;
  1096. }
  1097. }
  1098. }
  1099. switch (bcs->Status) {
  1100. case US_BULK_STAT_OK:
  1101. break;
  1102. case US_BULK_STAT_FAIL:
  1103. ub_state_sense(sc, cmd);
  1104. return;
  1105. case US_BULK_STAT_PHASE:
  1106. goto Bad_End;
  1107. default:
  1108. printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
  1109. sc->name, bcs->Status);
  1110. ub_state_done(sc, cmd, -EINVAL);
  1111. return;
  1112. }
  1113. /* Not zeroing error to preserve a babble indicator */
  1114. if (cmd->error != 0) {
  1115. ub_state_sense(sc, cmd);
  1116. return;
  1117. }
  1118. cmd->state = UB_CMDST_DONE;
  1119. ub_cmdq_pop(sc);
  1120. (*cmd->done)(sc, cmd);
  1121. } else if (cmd->state == UB_CMDST_SENSE) {
  1122. ub_state_done(sc, cmd, -EIO);
  1123. } else {
  1124. printk(KERN_WARNING "%s: wrong command state %d\n",
  1125. sc->name, cmd->state);
  1126. ub_state_done(sc, cmd, -EINVAL);
  1127. return;
  1128. }
  1129. return;
  1130. Bad_End: /* Little Excel is dead */
  1131. ub_state_done(sc, cmd, -EIO);
  1132. }
  1133. /*
  1134. * Factorization helper for the command state machine:
  1135. * Initiate a data segment transfer.
  1136. */
  1137. static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1138. {
  1139. struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
  1140. int pipe;
  1141. int rc;
  1142. UB_INIT_COMPLETION(sc->work_done);
  1143. if (cmd->dir == UB_DIR_READ)
  1144. pipe = sc->recv_bulk_pipe;
  1145. else
  1146. pipe = sc->send_bulk_pipe;
  1147. sc->last_pipe = pipe;
  1148. usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
  1149. sg->length, ub_urb_complete, sc);
  1150. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1151. /* XXX Clear stalls */
  1152. ub_complete(&sc->work_done);
  1153. ub_state_done(sc, cmd, rc);
  1154. return;
  1155. }
  1156. if (cmd->timeo)
  1157. sc->work_timer.expires = jiffies + cmd->timeo;
  1158. else
  1159. sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
  1160. add_timer(&sc->work_timer);
  1161. cmd->state = UB_CMDST_DATA;
  1162. }
  1163. /*
  1164. * Factorization helper for the command state machine:
  1165. * Finish the command.
  1166. */
  1167. static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
  1168. {
  1169. cmd->error = rc;
  1170. cmd->state = UB_CMDST_DONE;
  1171. ub_cmdq_pop(sc);
  1172. (*cmd->done)(sc, cmd);
  1173. }
  1174. /*
  1175. * Factorization helper for the command state machine:
  1176. * Submit a CSW read.
  1177. */
  1178. static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1179. {
  1180. int rc;
  1181. UB_INIT_COMPLETION(sc->work_done);
  1182. sc->last_pipe = sc->recv_bulk_pipe;
  1183. usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
  1184. &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
  1185. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1186. /* XXX Clear stalls */
  1187. ub_complete(&sc->work_done);
  1188. ub_state_done(sc, cmd, rc);
  1189. return -1;
  1190. }
  1191. if (cmd->timeo)
  1192. sc->work_timer.expires = jiffies + cmd->timeo;
  1193. else
  1194. sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
  1195. add_timer(&sc->work_timer);
  1196. return 0;
  1197. }
  1198. /*
  1199. * Factorization helper for the command state machine:
  1200. * Submit a CSW read and go to STAT state.
  1201. */
  1202. static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1203. {
  1204. if (__ub_state_stat(sc, cmd) != 0)
  1205. return;
  1206. cmd->stat_count = 0;
  1207. cmd->state = UB_CMDST_STAT;
  1208. }
  1209. /*
  1210. * Factorization helper for the command state machine:
  1211. * Submit a CSW read and go to STAT state with counter (along [C] path).
  1212. */
  1213. static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1214. {
  1215. if (++cmd->stat_count >= 4) {
  1216. ub_state_sense(sc, cmd);
  1217. return;
  1218. }
  1219. if (__ub_state_stat(sc, cmd) != 0)
  1220. return;
  1221. cmd->state = UB_CMDST_STAT;
  1222. }
  1223. /*
  1224. * Factorization helper for the command state machine:
  1225. * Submit a REQUEST SENSE and go to SENSE state.
  1226. */
  1227. static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1228. {
  1229. struct ub_scsi_cmd *scmd;
  1230. struct scatterlist *sg;
  1231. int rc;
  1232. if (cmd->cdb[0] == REQUEST_SENSE) {
  1233. rc = -EPIPE;
  1234. goto error;
  1235. }
  1236. scmd = &sc->top_rqs_cmd;
  1237. memset(scmd, 0, sizeof(struct ub_scsi_cmd));
  1238. scmd->cdb[0] = REQUEST_SENSE;
  1239. scmd->cdb[4] = UB_SENSE_SIZE;
  1240. scmd->cdb_len = 6;
  1241. scmd->dir = UB_DIR_READ;
  1242. scmd->state = UB_CMDST_INIT;
  1243. scmd->nsg = 1;
  1244. sg = &scmd->sgv[0];
  1245. sg_init_table(sg, UB_MAX_REQ_SG);
  1246. sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
  1247. (unsigned long)sc->top_sense & (PAGE_SIZE-1));
  1248. scmd->len = UB_SENSE_SIZE;
  1249. scmd->lun = cmd->lun;
  1250. scmd->done = ub_top_sense_done;
  1251. scmd->back = cmd;
  1252. scmd->tag = sc->tagcnt++;
  1253. cmd->state = UB_CMDST_SENSE;
  1254. ub_cmdq_insert(sc, scmd);
  1255. return;
  1256. error:
  1257. ub_state_done(sc, cmd, rc);
  1258. }
  1259. /*
  1260. * A helper for the command's state machine:
  1261. * Submit a stall clear.
  1262. */
  1263. static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
  1264. int stalled_pipe)
  1265. {
  1266. int endp;
  1267. struct usb_ctrlrequest *cr;
  1268. int rc;
  1269. endp = usb_pipeendpoint(stalled_pipe);
  1270. if (usb_pipein (stalled_pipe))
  1271. endp |= USB_DIR_IN;
  1272. cr = &sc->work_cr;
  1273. cr->bRequestType = USB_RECIP_ENDPOINT;
  1274. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1275. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1276. cr->wIndex = cpu_to_le16(endp);
  1277. cr->wLength = cpu_to_le16(0);
  1278. UB_INIT_COMPLETION(sc->work_done);
  1279. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1280. (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
  1281. if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
  1282. ub_complete(&sc->work_done);
  1283. return rc;
  1284. }
  1285. sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1286. add_timer(&sc->work_timer);
  1287. return 0;
  1288. }
  1289. /*
  1290. */
  1291. static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
  1292. {
  1293. unsigned char *sense = sc->top_sense;
  1294. struct ub_scsi_cmd *cmd;
  1295. /*
  1296. * Find the command which triggered the unit attention or a check,
  1297. * save the sense into it, and advance its state machine.
  1298. */
  1299. if ((cmd = ub_cmdq_peek(sc)) == NULL) {
  1300. printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
  1301. return;
  1302. }
  1303. if (cmd != scmd->back) {
  1304. printk(KERN_WARNING "%s: "
  1305. "sense done for wrong command 0x%x\n",
  1306. sc->name, cmd->tag);
  1307. return;
  1308. }
  1309. if (cmd->state != UB_CMDST_SENSE) {
  1310. printk(KERN_WARNING "%s: sense done with bad cmd state %d\n",
  1311. sc->name, cmd->state);
  1312. return;
  1313. }
  1314. /*
  1315. * Ignoring scmd->act_len, because the buffer was pre-zeroed.
  1316. */
  1317. cmd->key = sense[2] & 0x0F;
  1318. cmd->asc = sense[12];
  1319. cmd->ascq = sense[13];
  1320. ub_scsi_urb_compl(sc, cmd);
  1321. }
  1322. /*
  1323. * Reset management
  1324. */
  1325. static void ub_reset_enter(struct ub_dev *sc, int try)
  1326. {
  1327. if (sc->reset) {
  1328. /* This happens often on multi-LUN devices. */
  1329. return;
  1330. }
  1331. sc->reset = try + 1;
  1332. #if 0 /* Not needed because the disconnect waits for us. */
  1333. unsigned long flags;
  1334. spin_lock_irqsave(&ub_lock, flags);
  1335. sc->openc++;
  1336. spin_unlock_irqrestore(&ub_lock, flags);
  1337. #endif
  1338. #if 0 /* We let them stop themselves. */
  1339. struct ub_lun *lun;
  1340. list_for_each_entry(lun, &sc->luns, link) {
  1341. blk_stop_queue(lun->disk->queue);
  1342. }
  1343. #endif
  1344. schedule_work(&sc->reset_work);
  1345. }
  1346. static void ub_reset_task(struct work_struct *work)
  1347. {
  1348. struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
  1349. unsigned long flags;
  1350. struct ub_lun *lun;
  1351. int rc;
  1352. if (!sc->reset) {
  1353. printk(KERN_WARNING "%s: Running reset unrequested\n",
  1354. sc->name);
  1355. return;
  1356. }
  1357. if (atomic_read(&sc->poison)) {
  1358. ;
  1359. } else if ((sc->reset & 1) == 0) {
  1360. ub_sync_reset(sc);
  1361. msleep(700); /* usb-storage sleeps 6s (!) */
  1362. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  1363. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  1364. } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
  1365. ;
  1366. } else {
  1367. rc = usb_lock_device_for_reset(sc->dev, sc->intf);
  1368. if (rc < 0) {
  1369. printk(KERN_NOTICE
  1370. "%s: usb_lock_device_for_reset failed (%d)\n",
  1371. sc->name, rc);
  1372. } else {
  1373. rc = usb_reset_device(sc->dev);
  1374. if (rc < 0) {
  1375. printk(KERN_NOTICE "%s: "
  1376. "usb_lock_device_for_reset failed (%d)\n",
  1377. sc->name, rc);
  1378. }
  1379. usb_unlock_device(sc->dev);
  1380. }
  1381. }
  1382. /*
  1383. * In theory, no commands can be running while reset is active,
  1384. * so nobody can ask for another reset, and so we do not need any
  1385. * queues of resets or anything. We do need a spinlock though,
  1386. * to interact with block layer.
  1387. */
  1388. spin_lock_irqsave(sc->lock, flags);
  1389. sc->reset = 0;
  1390. tasklet_schedule(&sc->tasklet);
  1391. list_for_each_entry(lun, &sc->luns, link) {
  1392. blk_start_queue(lun->disk->queue);
  1393. }
  1394. wake_up(&sc->reset_wait);
  1395. spin_unlock_irqrestore(sc->lock, flags);
  1396. }
  1397. /*
  1398. * XXX Reset brackets are too much hassle to implement, so just stub them
  1399. * in order to prevent forced unbinding (which deadlocks solid when our
  1400. * ->disconnect method waits for the reset to complete and this kills keventd).
  1401. *
  1402. * XXX Tell Alan to move usb_unlock_device inside of usb_reset_device,
  1403. * or else the post_reset is invoked, and restats I/O on a locked device.
  1404. */
  1405. static int ub_pre_reset(struct usb_interface *iface) {
  1406. return 0;
  1407. }
  1408. static int ub_post_reset(struct usb_interface *iface) {
  1409. return 0;
  1410. }
  1411. /*
  1412. * This is called from a process context.
  1413. */
  1414. static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
  1415. {
  1416. lun->readonly = 0; /* XXX Query this from the device */
  1417. lun->capacity.nsec = 0;
  1418. lun->capacity.bsize = 512;
  1419. lun->capacity.bshift = 0;
  1420. if (ub_sync_tur(sc, lun) != 0)
  1421. return; /* Not ready */
  1422. lun->changed = 0;
  1423. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1424. /*
  1425. * The retry here means something is wrong, either with the
  1426. * device, with the transport, or with our code.
  1427. * We keep this because sd.c has retries for capacity.
  1428. */
  1429. if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
  1430. lun->capacity.nsec = 0;
  1431. lun->capacity.bsize = 512;
  1432. lun->capacity.bshift = 0;
  1433. }
  1434. }
  1435. }
  1436. /*
  1437. * The open funcion.
  1438. * This is mostly needed to keep refcounting, but also to support
  1439. * media checks on removable media drives.
  1440. */
  1441. static int ub_bd_open(struct block_device *bdev, fmode_t mode)
  1442. {
  1443. struct ub_lun *lun = bdev->bd_disk->private_data;
  1444. struct ub_dev *sc = lun->udev;
  1445. unsigned long flags;
  1446. int rc;
  1447. spin_lock_irqsave(&ub_lock, flags);
  1448. if (atomic_read(&sc->poison)) {
  1449. spin_unlock_irqrestore(&ub_lock, flags);
  1450. return -ENXIO;
  1451. }
  1452. sc->openc++;
  1453. spin_unlock_irqrestore(&ub_lock, flags);
  1454. if (lun->removable || lun->readonly)
  1455. check_disk_change(bdev);
  1456. /*
  1457. * The sd.c considers ->media_present and ->changed not equivalent,
  1458. * under some pretty murky conditions (a failure of READ CAPACITY).
  1459. * We may need it one day.
  1460. */
  1461. if (lun->removable && lun->changed && !(mode & FMODE_NDELAY)) {
  1462. rc = -ENOMEDIUM;
  1463. goto err_open;
  1464. }
  1465. if (lun->readonly && (mode & FMODE_WRITE)) {
  1466. rc = -EROFS;
  1467. goto err_open;
  1468. }
  1469. return 0;
  1470. err_open:
  1471. ub_put(sc);
  1472. return rc;
  1473. }
  1474. /*
  1475. */
  1476. static int ub_bd_release(struct gendisk *disk, fmode_t mode)
  1477. {
  1478. struct ub_lun *lun = disk->private_data;
  1479. struct ub_dev *sc = lun->udev;
  1480. ub_put(sc);
  1481. return 0;
  1482. }
  1483. /*
  1484. * The ioctl interface.
  1485. */
  1486. static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode,
  1487. unsigned int cmd, unsigned long arg)
  1488. {
  1489. struct gendisk *disk = bdev->bd_disk;
  1490. void __user *usermem = (void __user *) arg;
  1491. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem);
  1492. }
  1493. /*
  1494. * This is called by check_disk_change if we reported a media change.
  1495. * The main onjective here is to discover the features of the media such as
  1496. * the capacity, read-only status, etc. USB storage generally does not
  1497. * need to be spun up, but if we needed it, this would be the place.
  1498. *
  1499. * This call can sleep.
  1500. *
  1501. * The return code is not used.
  1502. */
  1503. static int ub_bd_revalidate(struct gendisk *disk)
  1504. {
  1505. struct ub_lun *lun = disk->private_data;
  1506. ub_revalidate(lun->udev, lun);
  1507. /* XXX Support sector size switching like in sr.c */
  1508. blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
  1509. set_capacity(disk, lun->capacity.nsec);
  1510. // set_disk_ro(sdkp->disk, lun->readonly);
  1511. return 0;
  1512. }
  1513. /*
  1514. * The check is called by the block layer to verify if the media
  1515. * is still available. It is supposed to be harmless, lightweight and
  1516. * non-intrusive in case the media was not changed.
  1517. *
  1518. * This call can sleep.
  1519. *
  1520. * The return code is bool!
  1521. */
  1522. static int ub_bd_media_changed(struct gendisk *disk)
  1523. {
  1524. struct ub_lun *lun = disk->private_data;
  1525. if (!lun->removable)
  1526. return 0;
  1527. /*
  1528. * We clean checks always after every command, so this is not
  1529. * as dangerous as it looks. If the TEST_UNIT_READY fails here,
  1530. * the device is actually not ready with operator or software
  1531. * intervention required. One dangerous item might be a drive which
  1532. * spins itself down, and come the time to write dirty pages, this
  1533. * will fail, then block layer discards the data. Since we never
  1534. * spin drives up, such devices simply cannot be used with ub anyway.
  1535. */
  1536. if (ub_sync_tur(lun->udev, lun) != 0) {
  1537. lun->changed = 1;
  1538. return 1;
  1539. }
  1540. return lun->changed;
  1541. }
  1542. static struct block_device_operations ub_bd_fops = {
  1543. .owner = THIS_MODULE,
  1544. .open = ub_bd_open,
  1545. .release = ub_bd_release,
  1546. .locked_ioctl = ub_bd_ioctl,
  1547. .media_changed = ub_bd_media_changed,
  1548. .revalidate_disk = ub_bd_revalidate,
  1549. };
  1550. /*
  1551. * Common ->done routine for commands executed synchronously.
  1552. */
  1553. static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
  1554. {
  1555. struct completion *cop = cmd->back;
  1556. complete(cop);
  1557. }
  1558. /*
  1559. * Test if the device has a check condition on it, synchronously.
  1560. */
  1561. static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
  1562. {
  1563. struct ub_scsi_cmd *cmd;
  1564. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
  1565. unsigned long flags;
  1566. struct completion compl;
  1567. int rc;
  1568. init_completion(&compl);
  1569. rc = -ENOMEM;
  1570. if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1571. goto err_alloc;
  1572. cmd->cdb[0] = TEST_UNIT_READY;
  1573. cmd->cdb_len = 6;
  1574. cmd->dir = UB_DIR_NONE;
  1575. cmd->state = UB_CMDST_INIT;
  1576. cmd->lun = lun; /* This may be NULL, but that's ok */
  1577. cmd->done = ub_probe_done;
  1578. cmd->back = &compl;
  1579. spin_lock_irqsave(sc->lock, flags);
  1580. cmd->tag = sc->tagcnt++;
  1581. rc = ub_submit_scsi(sc, cmd);
  1582. spin_unlock_irqrestore(sc->lock, flags);
  1583. if (rc != 0)
  1584. goto err_submit;
  1585. wait_for_completion(&compl);
  1586. rc = cmd->error;
  1587. if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
  1588. rc = cmd->key;
  1589. err_submit:
  1590. kfree(cmd);
  1591. err_alloc:
  1592. return rc;
  1593. }
  1594. /*
  1595. * Read the SCSI capacity synchronously (for probing).
  1596. */
  1597. static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
  1598. struct ub_capacity *ret)
  1599. {
  1600. struct ub_scsi_cmd *cmd;
  1601. struct scatterlist *sg;
  1602. char *p;
  1603. enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
  1604. unsigned long flags;
  1605. unsigned int bsize, shift;
  1606. unsigned long nsec;
  1607. struct completion compl;
  1608. int rc;
  1609. init_completion(&compl);
  1610. rc = -ENOMEM;
  1611. if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1612. goto err_alloc;
  1613. p = (char *)cmd + sizeof(struct ub_scsi_cmd);
  1614. cmd->cdb[0] = 0x25;
  1615. cmd->cdb_len = 10;
  1616. cmd->dir = UB_DIR_READ;
  1617. cmd->state = UB_CMDST_INIT;
  1618. cmd->nsg = 1;
  1619. sg = &cmd->sgv[0];
  1620. sg_init_table(sg, UB_MAX_REQ_SG);
  1621. sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
  1622. cmd->len = 8;
  1623. cmd->lun = lun;
  1624. cmd->done = ub_probe_done;
  1625. cmd->back = &compl;
  1626. spin_lock_irqsave(sc->lock, flags);
  1627. cmd->tag = sc->tagcnt++;
  1628. rc = ub_submit_scsi(sc, cmd);
  1629. spin_unlock_irqrestore(sc->lock, flags);
  1630. if (rc != 0)
  1631. goto err_submit;
  1632. wait_for_completion(&compl);
  1633. if (cmd->error != 0) {
  1634. rc = -EIO;
  1635. goto err_read;
  1636. }
  1637. if (cmd->act_len != 8) {
  1638. rc = -EIO;
  1639. goto err_read;
  1640. }
  1641. /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
  1642. nsec = be32_to_cpu(*(__be32 *)p) + 1;
  1643. bsize = be32_to_cpu(*(__be32 *)(p + 4));
  1644. switch (bsize) {
  1645. case 512: shift = 0; break;
  1646. case 1024: shift = 1; break;
  1647. case 2048: shift = 2; break;
  1648. case 4096: shift = 3; break;
  1649. default:
  1650. rc = -EDOM;
  1651. goto err_inv_bsize;
  1652. }
  1653. ret->bsize = bsize;
  1654. ret->bshift = shift;
  1655. ret->nsec = nsec << shift;
  1656. rc = 0;
  1657. err_inv_bsize:
  1658. err_read:
  1659. err_submit:
  1660. kfree(cmd);
  1661. err_alloc:
  1662. return rc;
  1663. }
  1664. /*
  1665. */
  1666. static void ub_probe_urb_complete(struct urb *urb)
  1667. {
  1668. struct completion *cop = urb->context;
  1669. complete(cop);
  1670. }
  1671. static void ub_probe_timeout(unsigned long arg)
  1672. {
  1673. struct completion *cop = (struct completion *) arg;
  1674. complete(cop);
  1675. }
  1676. /*
  1677. * Reset with a Bulk reset.
  1678. */
  1679. static int ub_sync_reset(struct ub_dev *sc)
  1680. {
  1681. int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
  1682. struct usb_ctrlrequest *cr;
  1683. struct completion compl;
  1684. struct timer_list timer;
  1685. int rc;
  1686. init_completion(&compl);
  1687. cr = &sc->work_cr;
  1688. cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1689. cr->bRequest = US_BULK_RESET_REQUEST;
  1690. cr->wValue = cpu_to_le16(0);
  1691. cr->wIndex = cpu_to_le16(ifnum);
  1692. cr->wLength = cpu_to_le16(0);
  1693. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1694. (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
  1695. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1696. printk(KERN_WARNING
  1697. "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
  1698. return rc;
  1699. }
  1700. init_timer(&timer);
  1701. timer.function = ub_probe_timeout;
  1702. timer.data = (unsigned long) &compl;
  1703. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1704. add_timer(&timer);
  1705. wait_for_completion(&compl);
  1706. del_timer_sync(&timer);
  1707. usb_kill_urb(&sc->work_urb);
  1708. return sc->work_urb.status;
  1709. }
  1710. /*
  1711. * Get number of LUNs by the way of Bulk GetMaxLUN command.
  1712. */
  1713. static int ub_sync_getmaxlun(struct ub_dev *sc)
  1714. {
  1715. int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
  1716. unsigned char *p;
  1717. enum { ALLOC_SIZE = 1 };
  1718. struct usb_ctrlrequest *cr;
  1719. struct completion compl;
  1720. struct timer_list timer;
  1721. int nluns;
  1722. int rc;
  1723. init_completion(&compl);
  1724. rc = -ENOMEM;
  1725. if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
  1726. goto err_alloc;
  1727. *p = 55;
  1728. cr = &sc->work_cr;
  1729. cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  1730. cr->bRequest = US_BULK_GET_MAX_LUN;
  1731. cr->wValue = cpu_to_le16(0);
  1732. cr->wIndex = cpu_to_le16(ifnum);
  1733. cr->wLength = cpu_to_le16(1);
  1734. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
  1735. (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
  1736. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
  1737. goto err_submit;
  1738. init_timer(&timer);
  1739. timer.function = ub_probe_timeout;
  1740. timer.data = (unsigned long) &compl;
  1741. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1742. add_timer(&timer);
  1743. wait_for_completion(&compl);
  1744. del_timer_sync(&timer);
  1745. usb_kill_urb(&sc->work_urb);
  1746. if ((rc = sc->work_urb.status) < 0)
  1747. goto err_io;
  1748. if (sc->work_urb.actual_length != 1) {
  1749. nluns = 0;
  1750. } else {
  1751. if ((nluns = *p) == 55) {
  1752. nluns = 0;
  1753. } else {
  1754. /* GetMaxLUN returns the maximum LUN number */
  1755. nluns += 1;
  1756. if (nluns > UB_MAX_LUNS)
  1757. nluns = UB_MAX_LUNS;
  1758. }
  1759. }
  1760. kfree(p);
  1761. return nluns;
  1762. err_io:
  1763. err_submit:
  1764. kfree(p);
  1765. err_alloc:
  1766. return rc;
  1767. }
  1768. /*
  1769. * Clear initial stalls.
  1770. */
  1771. static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
  1772. {
  1773. int endp;
  1774. struct usb_ctrlrequest *cr;
  1775. struct completion compl;
  1776. struct timer_list timer;
  1777. int rc;
  1778. init_completion(&compl);
  1779. endp = usb_pipeendpoint(stalled_pipe);
  1780. if (usb_pipein (stalled_pipe))
  1781. endp |= USB_DIR_IN;
  1782. cr = &sc->work_cr;
  1783. cr->bRequestType = USB_RECIP_ENDPOINT;
  1784. cr->bRequest = USB_REQ_CLEAR_FEATURE;
  1785. cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
  1786. cr->wIndex = cpu_to_le16(endp);
  1787. cr->wLength = cpu_to_le16(0);
  1788. usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
  1789. (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
  1790. if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
  1791. printk(KERN_WARNING
  1792. "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
  1793. return rc;
  1794. }
  1795. init_timer(&timer);
  1796. timer.function = ub_probe_timeout;
  1797. timer.data = (unsigned long) &compl;
  1798. timer.expires = jiffies + UB_CTRL_TIMEOUT;
  1799. add_timer(&timer);
  1800. wait_for_completion(&compl);
  1801. del_timer_sync(&timer);
  1802. usb_kill_urb(&sc->work_urb);
  1803. usb_reset_endpoint(sc->dev, endp);
  1804. return 0;
  1805. }
  1806. /*
  1807. * Get the pipe settings.
  1808. */
  1809. static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
  1810. struct usb_interface *intf)
  1811. {
  1812. struct usb_host_interface *altsetting = intf->cur_altsetting;
  1813. struct usb_endpoint_descriptor *ep_in = NULL;
  1814. struct usb_endpoint_descriptor *ep_out = NULL;
  1815. struct usb_endpoint_descriptor *ep;
  1816. int i;
  1817. /*
  1818. * Find the endpoints we need.
  1819. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1820. * We will ignore any others.
  1821. */
  1822. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  1823. ep = &altsetting->endpoint[i].desc;
  1824. /* Is it a BULK endpoint? */
  1825. if (usb_endpoint_xfer_bulk(ep)) {
  1826. /* BULK in or out? */
  1827. if (usb_endpoint_dir_in(ep)) {
  1828. if (ep_in == NULL)
  1829. ep_in = ep;
  1830. } else {
  1831. if (ep_out == NULL)
  1832. ep_out = ep;
  1833. }
  1834. }
  1835. }
  1836. if (ep_in == NULL || ep_out == NULL) {
  1837. printk(KERN_NOTICE "%s: failed endpoint check\n", sc->name);
  1838. return -ENODEV;
  1839. }
  1840. /* Calculate and store the pipe values */
  1841. sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
  1842. sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
  1843. sc->send_bulk_pipe = usb_sndbulkpipe(dev,
  1844. usb_endpoint_num(ep_out));
  1845. sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
  1846. usb_endpoint_num(ep_in));
  1847. return 0;
  1848. }
  1849. /*
  1850. * Probing is done in the process context, which allows us to cheat
  1851. * and not to build a state machine for the discovery.
  1852. */
  1853. static int ub_probe(struct usb_interface *intf,
  1854. const struct usb_device_id *dev_id)
  1855. {
  1856. struct ub_dev *sc;
  1857. int nluns;
  1858. int rc;
  1859. int i;
  1860. if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
  1861. return -ENXIO;
  1862. rc = -ENOMEM;
  1863. if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
  1864. goto err_core;
  1865. sc->lock = ub_next_lock();
  1866. INIT_LIST_HEAD(&sc->luns);
  1867. usb_init_urb(&sc->work_urb);
  1868. tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
  1869. atomic_set(&sc->poison, 0);
  1870. INIT_WORK(&sc->reset_work, ub_reset_task);
  1871. init_waitqueue_head(&sc->reset_wait);
  1872. init_timer(&sc->work_timer);
  1873. sc->work_timer.data = (unsigned long) sc;
  1874. sc->work_timer.function = ub_urb_timeout;
  1875. ub_init_completion(&sc->work_done);
  1876. sc->work_done.done = 1; /* A little yuk, but oh well... */
  1877. sc->dev = interface_to_usbdev(intf);
  1878. sc->intf = intf;
  1879. // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  1880. usb_set_intfdata(intf, sc);
  1881. usb_get_dev(sc->dev);
  1882. /*
  1883. * Since we give the interface struct to the block level through
  1884. * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
  1885. * oopses on close after a disconnect (kernels 2.6.16 and up).
  1886. */
  1887. usb_get_intf(sc->intf);
  1888. snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
  1889. sc->dev->bus->busnum, sc->dev->devnum);
  1890. /* XXX Verify that we can handle the device (from descriptors) */
  1891. if (ub_get_pipes(sc, sc->dev, intf) != 0)
  1892. goto err_dev_desc;
  1893. /*
  1894. * At this point, all USB initialization is done, do upper layer.
  1895. * We really hate halfway initialized structures, so from the
  1896. * invariants perspective, this ub_dev is fully constructed at
  1897. * this point.
  1898. */
  1899. /*
  1900. * This is needed to clear toggles. It is a problem only if we do
  1901. * `rmmod ub && modprobe ub` without disconnects, but we like that.
  1902. */
  1903. #if 0 /* iPod Mini fails if we do this (big white iPod works) */
  1904. ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
  1905. ub_probe_clear_stall(sc, sc->send_bulk_pipe);
  1906. #endif
  1907. /*
  1908. * The way this is used by the startup code is a little specific.
  1909. * A SCSI check causes a USB stall. Our common case code sees it
  1910. * and clears the check, after which the device is ready for use.
  1911. * But if a check was not present, any command other than
  1912. * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
  1913. *
  1914. * If we neglect to clear the SCSI check, the first real command fails
  1915. * (which is the capacity readout). We clear that and retry, but why
  1916. * causing spurious retries for no reason.
  1917. *
  1918. * Revalidation may start with its own TEST_UNIT_READY, but that one
  1919. * has to succeed, so we clear checks with an additional one here.
  1920. * In any case it's not our business how revaliadation is implemented.
  1921. */
  1922. for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
  1923. if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
  1924. if (rc != 0x6) break;
  1925. msleep(10);
  1926. }
  1927. nluns = 1;
  1928. for (i = 0; i < 3; i++) {
  1929. if ((rc = ub_sync_getmaxlun(sc)) < 0)
  1930. break;
  1931. if (rc != 0) {
  1932. nluns = rc;
  1933. break;
  1934. }
  1935. msleep(100);
  1936. }
  1937. for (i = 0; i < nluns; i++) {
  1938. ub_probe_lun(sc, i);
  1939. }
  1940. return 0;
  1941. err_dev_desc:
  1942. usb_set_intfdata(intf, NULL);
  1943. usb_put_intf(sc->intf);
  1944. usb_put_dev(sc->dev);
  1945. kfree(sc);
  1946. err_core:
  1947. return rc;
  1948. }
  1949. static int ub_probe_lun(struct ub_dev *sc, int lnum)
  1950. {
  1951. struct ub_lun *lun;
  1952. struct request_queue *q;
  1953. struct gendisk *disk;
  1954. int rc;
  1955. rc = -ENOMEM;
  1956. if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
  1957. goto err_alloc;
  1958. lun->num = lnum;
  1959. rc = -ENOSR;
  1960. if ((lun->id = ub_id_get()) == -1)
  1961. goto err_id;
  1962. lun->udev = sc;
  1963. snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
  1964. lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
  1965. lun->removable = 1; /* XXX Query this from the device */
  1966. lun->changed = 1; /* ub_revalidate clears only */
  1967. ub_revalidate(sc, lun);
  1968. rc = -ENOMEM;
  1969. if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
  1970. goto err_diskalloc;
  1971. sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
  1972. disk->major = UB_MAJOR;
  1973. disk->first_minor = lun->id * UB_PARTS_PER_LUN;
  1974. disk->fops = &ub_bd_fops;
  1975. disk->private_data = lun;
  1976. disk->driverfs_dev = &sc->intf->dev;
  1977. rc = -ENOMEM;
  1978. if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
  1979. goto err_blkqinit;
  1980. disk->queue = q;
  1981. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  1982. blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
  1983. blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
  1984. blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
  1985. blk_queue_max_sectors(q, UB_MAX_SECTORS);
  1986. blk_queue_hardsect_size(q, lun->capacity.bsize);
  1987. lun->disk = disk;
  1988. q->queuedata = lun;
  1989. list_add(&lun->link, &sc->luns);
  1990. set_capacity(disk, lun->capacity.nsec);
  1991. if (lun->removable)
  1992. disk->flags |= GENHD_FL_REMOVABLE;
  1993. add_disk(disk);
  1994. return 0;
  1995. err_blkqinit:
  1996. put_disk(disk);
  1997. err_diskalloc:
  1998. ub_id_put(lun->id);
  1999. err_id:
  2000. kfree(lun);
  2001. err_alloc:
  2002. return rc;
  2003. }
  2004. static void ub_disconnect(struct usb_interface *intf)
  2005. {
  2006. struct ub_dev *sc = usb_get_intfdata(intf);
  2007. struct ub_lun *lun;
  2008. unsigned long flags;
  2009. /*
  2010. * Prevent ub_bd_release from pulling the rug from under us.
  2011. * XXX This is starting to look like a kref.
  2012. * XXX Why not to take this ref at probe time?
  2013. */
  2014. spin_lock_irqsave(&ub_lock, flags);
  2015. sc->openc++;
  2016. spin_unlock_irqrestore(&ub_lock, flags);
  2017. /*
  2018. * Fence stall clearings, operations triggered by unlinkings and so on.
  2019. * We do not attempt to unlink any URBs, because we do not trust the
  2020. * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
  2021. */
  2022. atomic_set(&sc->poison, 1);
  2023. /*
  2024. * Wait for reset to end, if any.
  2025. */
  2026. wait_event(sc->reset_wait, !sc->reset);
  2027. /*
  2028. * Blow away queued commands.
  2029. *
  2030. * Actually, this never works, because before we get here
  2031. * the HCD terminates outstanding URB(s). It causes our
  2032. * SCSI command queue to advance, commands fail to submit,
  2033. * and the whole queue drains. So, we just use this code to
  2034. * print warnings.
  2035. */
  2036. spin_lock_irqsave(sc->lock, flags);
  2037. {
  2038. struct ub_scsi_cmd *cmd;
  2039. int cnt = 0;
  2040. while ((cmd = ub_cmdq_peek(sc)) != NULL) {
  2041. cmd->error = -ENOTCONN;
  2042. cmd->state = UB_CMDST_DONE;
  2043. ub_cmdq_pop(sc);
  2044. (*cmd->done)(sc, cmd);
  2045. cnt++;
  2046. }
  2047. if (cnt != 0) {
  2048. printk(KERN_WARNING "%s: "
  2049. "%d was queued after shutdown\n", sc->name, cnt);
  2050. }
  2051. }
  2052. spin_unlock_irqrestore(sc->lock, flags);
  2053. /*
  2054. * Unregister the upper layer.
  2055. */
  2056. list_for_each_entry(lun, &sc->luns, link) {
  2057. del_gendisk(lun->disk);
  2058. /*
  2059. * I wish I could do:
  2060. * queue_flag_set(QUEUE_FLAG_DEAD, q);
  2061. * As it is, we rely on our internal poisoning and let
  2062. * the upper levels to spin furiously failing all the I/O.
  2063. */
  2064. }
  2065. /*
  2066. * Testing for -EINPROGRESS is always a bug, so we are bending
  2067. * the rules a little.
  2068. */
  2069. spin_lock_irqsave(sc->lock, flags);
  2070. if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
  2071. printk(KERN_WARNING "%s: "
  2072. "URB is active after disconnect\n", sc->name);
  2073. }
  2074. spin_unlock_irqrestore(sc->lock, flags);
  2075. /*
  2076. * There is virtually no chance that other CPU runs a timeout so long
  2077. * after ub_urb_complete should have called del_timer, but only if HCD
  2078. * didn't forget to deliver a callback on unlink.
  2079. */
  2080. del_timer_sync(&sc->work_timer);
  2081. /*
  2082. * At this point there must be no commands coming from anyone
  2083. * and no URBs left in transit.
  2084. */
  2085. ub_put(sc);
  2086. }
  2087. static struct usb_driver ub_driver = {
  2088. .name = "ub",
  2089. .probe = ub_probe,
  2090. .disconnect = ub_disconnect,
  2091. .id_table = ub_usb_ids,
  2092. .pre_reset = ub_pre_reset,
  2093. .post_reset = ub_post_reset,
  2094. };
  2095. static int __init ub_init(void)
  2096. {
  2097. int rc;
  2098. int i;
  2099. for (i = 0; i < UB_QLOCK_NUM; i++)
  2100. spin_lock_init(&ub_qlockv[i]);
  2101. if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
  2102. goto err_regblkdev;
  2103. if ((rc = usb_register(&ub_driver)) != 0)
  2104. goto err_register;
  2105. usb_usual_set_present(USB_US_TYPE_UB);
  2106. return 0;
  2107. err_register:
  2108. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2109. err_regblkdev:
  2110. return rc;
  2111. }
  2112. static void __exit ub_exit(void)
  2113. {
  2114. usb_deregister(&ub_driver);
  2115. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2116. usb_usual_clear_present(USB_US_TYPE_UB);
  2117. }
  2118. module_init(ub_init);
  2119. module_exit(ub_exit);
  2120. MODULE_LICENSE("GPL");