ub.c 62 KB

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