ub.c 61 KB

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