storage_common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * storage_common.c -- Common definitions for mass storage functionality
  3. *
  4. * Copyright (C) 2003-2008 Alan Stern
  5. * Copyeight (C) 2009 Samsung Electronics
  6. * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. * This file requires the following identifiers used in USB strings to
  24. * be defined (each of type pointer to char):
  25. * - fsg_string_manufacturer -- name of the manufacturer
  26. * - fsg_string_product -- name of the product
  27. * - fsg_string_serial -- product's serial
  28. * - fsg_string_config -- name of the configuration
  29. * - fsg_string_interface -- name of the interface
  30. * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
  31. * macro is defined prior to including this file.
  32. */
  33. #include <asm/unaligned.h>
  34. /*-------------------------------------------------------------------------*/
  35. #ifndef DEBUG
  36. #undef VERBOSE_DEBUG
  37. #undef DUMP_MSGS
  38. #endif /* !DEBUG */
  39. #ifdef VERBOSE_DEBUG
  40. #define VLDBG LDBG
  41. #else
  42. #define VLDBG(lun, fmt, args...) do { } while (0)
  43. #endif /* VERBOSE_DEBUG */
  44. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  45. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  46. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  47. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  48. #define DBG(d, fmt, args...) dev_dbg (&(d)->gadget->dev, fmt, ## args)
  49. #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev, fmt, ## args)
  50. #define ERROR(d, fmt, args...) dev_err (&(d)->gadget->dev, fmt, ## args)
  51. #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev, fmt, ## args)
  52. #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev, fmt, ## args)
  53. #ifdef DUMP_MSGS
  54. # define dump_msg(fsg, /* const char * */ label, \
  55. /* const u8 * */ buf, /* unsigned */ length) do { \
  56. if (length < 512) { \
  57. DBG(fsg, "%s, length %u:\n", label, length); \
  58. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  59. 16, 1, buf, length, 0); \
  60. } \
  61. } while (0)
  62. # define dump_cdb(fsg) do { } while (0)
  63. #else
  64. # define dump_msg(fsg, /* const char * */ label, \
  65. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  66. # ifdef VERBOSE_DEBUG
  67. #define dump_cdb(fsg) \
  68. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  69. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  70. # else
  71. # define dump_cdb(fsg) do { } while (0)
  72. # endif /* VERBOSE_DEBUG */
  73. #endif /* DUMP_MSGS */
  74. /*-------------------------------------------------------------------------*/
  75. /* SCSI device types */
  76. #define TYPE_DISK 0x00
  77. #define TYPE_CDROM 0x05
  78. /* USB protocol value = the transport method */
  79. #define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
  80. #define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
  81. #define USB_PR_BULK 0x50 // Bulk-only
  82. /* USB subclass value = the protocol encapsulation */
  83. #define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
  84. #define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
  85. #define USB_SC_QIC 0x03 // QIC-157 (tape)
  86. #define USB_SC_UFI 0x04 // UFI (floppy)
  87. #define USB_SC_8070 0x05 // SFF-8070i (removable)
  88. #define USB_SC_SCSI 0x06 // Transparent SCSI
  89. /* Bulk-only data structures */
  90. /* Command Block Wrapper */
  91. struct fsg_bulk_cb_wrap {
  92. __le32 Signature; // Contains 'USBC'
  93. u32 Tag; // Unique per command id
  94. __le32 DataTransferLength; // Size of the data
  95. u8 Flags; // Direction in bit 7
  96. u8 Lun; // LUN (normally 0)
  97. u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
  98. u8 CDB[16]; // Command Data Block
  99. };
  100. #define USB_BULK_CB_WRAP_LEN 31
  101. #define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
  102. #define USB_BULK_IN_FLAG 0x80
  103. /* Command Status Wrapper */
  104. struct bulk_cs_wrap {
  105. __le32 Signature; // Should = 'USBS'
  106. u32 Tag; // Same as original command
  107. __le32 Residue; // Amount not transferred
  108. u8 Status; // See below
  109. };
  110. #define USB_BULK_CS_WRAP_LEN 13
  111. #define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
  112. #define USB_STATUS_PASS 0
  113. #define USB_STATUS_FAIL 1
  114. #define USB_STATUS_PHASE_ERROR 2
  115. /* Bulk-only class specific requests */
  116. #define USB_BULK_RESET_REQUEST 0xff
  117. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  118. /* CBI Interrupt data structure */
  119. struct interrupt_data {
  120. u8 bType;
  121. u8 bValue;
  122. };
  123. #define CBI_INTERRUPT_DATA_LEN 2
  124. /* CBI Accept Device-Specific Command request */
  125. #define USB_CBI_ADSC_REQUEST 0x00
  126. #define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
  127. /* SCSI commands that we recognize */
  128. #define SC_FORMAT_UNIT 0x04
  129. #define SC_INQUIRY 0x12
  130. #define SC_MODE_SELECT_6 0x15
  131. #define SC_MODE_SELECT_10 0x55
  132. #define SC_MODE_SENSE_6 0x1a
  133. #define SC_MODE_SENSE_10 0x5a
  134. #define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
  135. #define SC_READ_6 0x08
  136. #define SC_READ_10 0x28
  137. #define SC_READ_12 0xa8
  138. #define SC_READ_CAPACITY 0x25
  139. #define SC_READ_FORMAT_CAPACITIES 0x23
  140. #define SC_READ_HEADER 0x44
  141. #define SC_READ_TOC 0x43
  142. #define SC_RELEASE 0x17
  143. #define SC_REQUEST_SENSE 0x03
  144. #define SC_RESERVE 0x16
  145. #define SC_SEND_DIAGNOSTIC 0x1d
  146. #define SC_START_STOP_UNIT 0x1b
  147. #define SC_SYNCHRONIZE_CACHE 0x35
  148. #define SC_TEST_UNIT_READY 0x00
  149. #define SC_VERIFY 0x2f
  150. #define SC_WRITE_6 0x0a
  151. #define SC_WRITE_10 0x2a
  152. #define SC_WRITE_12 0xaa
  153. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  154. #define SS_NO_SENSE 0
  155. #define SS_COMMUNICATION_FAILURE 0x040800
  156. #define SS_INVALID_COMMAND 0x052000
  157. #define SS_INVALID_FIELD_IN_CDB 0x052400
  158. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  159. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  160. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  161. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  162. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  163. #define SS_RESET_OCCURRED 0x062900
  164. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  165. #define SS_UNRECOVERED_READ_ERROR 0x031100
  166. #define SS_WRITE_ERROR 0x030c02
  167. #define SS_WRITE_PROTECTED 0x072700
  168. #define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
  169. #define ASC(x) ((u8) ((x) >> 8))
  170. #define ASCQ(x) ((u8) (x))
  171. /*-------------------------------------------------------------------------*/
  172. struct fsg_lun {
  173. struct file *filp;
  174. loff_t file_length;
  175. loff_t num_sectors;
  176. unsigned int initially_ro : 1;
  177. unsigned int ro : 1;
  178. unsigned int removable : 1;
  179. unsigned int cdrom : 1;
  180. unsigned int prevent_medium_removal : 1;
  181. unsigned int registered : 1;
  182. unsigned int info_valid : 1;
  183. u32 sense_data;
  184. u32 sense_data_info;
  185. u32 unit_attention_data;
  186. struct device dev;
  187. };
  188. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  189. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  190. {
  191. return container_of(dev, struct fsg_lun, dev);
  192. }
  193. /* Big enough to hold our biggest descriptor */
  194. #define EP0_BUFSIZE 256
  195. #define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
  196. /* Number of buffers we will use. 2 is enough for double-buffering */
  197. #define FSG_NUM_BUFFERS 2
  198. enum fsg_buffer_state {
  199. BUF_STATE_EMPTY = 0,
  200. BUF_STATE_FULL,
  201. BUF_STATE_BUSY
  202. };
  203. struct fsg_buffhd {
  204. void *buf;
  205. enum fsg_buffer_state state;
  206. struct fsg_buffhd *next;
  207. /* The NetChip 2280 is faster, and handles some protocol faults
  208. * better, if we don't submit any short bulk-out read requests.
  209. * So we will record the intended request length here. */
  210. unsigned int bulk_out_intended_length;
  211. struct usb_request *inreq;
  212. int inreq_busy;
  213. struct usb_request *outreq;
  214. int outreq_busy;
  215. };
  216. enum fsg_state {
  217. FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
  218. FSG_STATE_DATA_PHASE,
  219. FSG_STATE_STATUS_PHASE,
  220. FSG_STATE_IDLE = 0,
  221. FSG_STATE_ABORT_BULK_OUT,
  222. FSG_STATE_RESET,
  223. FSG_STATE_INTERFACE_CHANGE,
  224. FSG_STATE_CONFIG_CHANGE,
  225. FSG_STATE_DISCONNECT,
  226. FSG_STATE_EXIT,
  227. FSG_STATE_TERMINATED
  228. };
  229. enum data_direction {
  230. DATA_DIR_UNKNOWN = 0,
  231. DATA_DIR_FROM_HOST,
  232. DATA_DIR_TO_HOST,
  233. DATA_DIR_NONE
  234. };
  235. /*-------------------------------------------------------------------------*/
  236. static inline u32 get_unaligned_be24(u8 *buf)
  237. {
  238. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  239. }
  240. /*-------------------------------------------------------------------------*/
  241. enum {
  242. FSG_STRING_MANUFACTURER = 1,
  243. FSG_STRING_PRODUCT,
  244. FSG_STRING_SERIAL,
  245. FSG_STRING_CONFIG,
  246. FSG_STRING_INTERFACE
  247. };
  248. static struct usb_otg_descriptor
  249. fsg_otg_desc = {
  250. .bLength = sizeof fsg_otg_desc,
  251. .bDescriptorType = USB_DT_OTG,
  252. .bmAttributes = USB_OTG_SRP,
  253. };
  254. /* There is only one interface. */
  255. static struct usb_interface_descriptor
  256. fsg_intf_desc = {
  257. .bLength = sizeof fsg_intf_desc,
  258. .bDescriptorType = USB_DT_INTERFACE,
  259. .bNumEndpoints = 2, // Adjusted during fsg_bind()
  260. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  261. .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
  262. .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
  263. .iInterface = FSG_STRING_INTERFACE,
  264. };
  265. /* Three full-speed endpoint descriptors: bulk-in, bulk-out,
  266. * and interrupt-in. */
  267. static struct usb_endpoint_descriptor
  268. fsg_fs_bulk_in_desc = {
  269. .bLength = USB_DT_ENDPOINT_SIZE,
  270. .bDescriptorType = USB_DT_ENDPOINT,
  271. .bEndpointAddress = USB_DIR_IN,
  272. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  273. /* wMaxPacketSize set by autoconfiguration */
  274. };
  275. static struct usb_endpoint_descriptor
  276. fsg_fs_bulk_out_desc = {
  277. .bLength = USB_DT_ENDPOINT_SIZE,
  278. .bDescriptorType = USB_DT_ENDPOINT,
  279. .bEndpointAddress = USB_DIR_OUT,
  280. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  281. /* wMaxPacketSize set by autoconfiguration */
  282. };
  283. static struct usb_endpoint_descriptor
  284. fsg_fs_intr_in_desc = {
  285. .bLength = USB_DT_ENDPOINT_SIZE,
  286. .bDescriptorType = USB_DT_ENDPOINT,
  287. .bEndpointAddress = USB_DIR_IN,
  288. .bmAttributes = USB_ENDPOINT_XFER_INT,
  289. .wMaxPacketSize = cpu_to_le16(2),
  290. .bInterval = 32, // frames -> 32 ms
  291. };
  292. static const struct usb_descriptor_header *fsg_fs_function[] = {
  293. (struct usb_descriptor_header *) &fsg_otg_desc,
  294. (struct usb_descriptor_header *) &fsg_intf_desc,
  295. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  296. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  297. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  298. NULL,
  299. };
  300. #define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  301. /*
  302. * USB 2.0 devices need to expose both high speed and full speed
  303. * descriptors, unless they only run at full speed.
  304. *
  305. * That means alternate endpoint descriptors (bigger packets)
  306. * and a "device qualifier" ... plus more construction options
  307. * for the config descriptor.
  308. */
  309. static struct usb_endpoint_descriptor
  310. fsg_hs_bulk_in_desc = {
  311. .bLength = USB_DT_ENDPOINT_SIZE,
  312. .bDescriptorType = USB_DT_ENDPOINT,
  313. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  314. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  315. .wMaxPacketSize = cpu_to_le16(512),
  316. };
  317. static struct usb_endpoint_descriptor
  318. fsg_hs_bulk_out_desc = {
  319. .bLength = USB_DT_ENDPOINT_SIZE,
  320. .bDescriptorType = USB_DT_ENDPOINT,
  321. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  322. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  323. .wMaxPacketSize = cpu_to_le16(512),
  324. .bInterval = 1, // NAK every 1 uframe
  325. };
  326. static struct usb_endpoint_descriptor
  327. fsg_hs_intr_in_desc = {
  328. .bLength = USB_DT_ENDPOINT_SIZE,
  329. .bDescriptorType = USB_DT_ENDPOINT,
  330. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  331. .bmAttributes = USB_ENDPOINT_XFER_INT,
  332. .wMaxPacketSize = cpu_to_le16(2),
  333. .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
  334. };
  335. static const struct usb_descriptor_header *fsg_hs_function[] = {
  336. (struct usb_descriptor_header *) &fsg_otg_desc,
  337. (struct usb_descriptor_header *) &fsg_intf_desc,
  338. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  339. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  340. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  341. NULL,
  342. };
  343. #define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  344. /* Maxpacket and other transfer characteristics vary by speed. */
  345. static struct usb_endpoint_descriptor *
  346. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  347. struct usb_endpoint_descriptor *hs)
  348. {
  349. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  350. return hs;
  351. return fs;
  352. }
  353. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  354. static struct usb_string fsg_strings[] = {
  355. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  356. {FSG_STRING_PRODUCT, fsg_string_product},
  357. {FSG_STRING_SERIAL, fsg_string_serial},
  358. {FSG_STRING_CONFIG, fsg_string_config},
  359. {FSG_STRING_INTERFACE, fsg_string_interface},
  360. {}
  361. };
  362. static struct usb_gadget_strings fsg_stringtab = {
  363. .language = 0x0409, // en-us
  364. .strings = fsg_strings,
  365. };
  366. /*-------------------------------------------------------------------------*/
  367. /* If the next two routines are called while the gadget is registered,
  368. * the caller must own fsg->filesem for writing. */
  369. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  370. {
  371. int ro;
  372. struct file *filp = NULL;
  373. int rc = -EINVAL;
  374. struct inode *inode = NULL;
  375. loff_t size;
  376. loff_t num_sectors;
  377. loff_t min_sectors;
  378. /* R/W if we can, R/O if we must */
  379. ro = curlun->initially_ro;
  380. if (!ro) {
  381. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  382. if (-EROFS == PTR_ERR(filp))
  383. ro = 1;
  384. }
  385. if (ro)
  386. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  387. if (IS_ERR(filp)) {
  388. LINFO(curlun, "unable to open backing file: %s\n", filename);
  389. return PTR_ERR(filp);
  390. }
  391. if (!(filp->f_mode & FMODE_WRITE))
  392. ro = 1;
  393. if (filp->f_path.dentry)
  394. inode = filp->f_path.dentry->d_inode;
  395. if (inode && S_ISBLK(inode->i_mode)) {
  396. if (bdev_read_only(inode->i_bdev))
  397. ro = 1;
  398. } else if (!inode || !S_ISREG(inode->i_mode)) {
  399. LINFO(curlun, "invalid file type: %s\n", filename);
  400. goto out;
  401. }
  402. /* If we can't read the file, it's no good.
  403. * If we can't write the file, use it read-only. */
  404. if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
  405. LINFO(curlun, "file not readable: %s\n", filename);
  406. goto out;
  407. }
  408. if (!(filp->f_op->write || filp->f_op->aio_write))
  409. ro = 1;
  410. size = i_size_read(inode->i_mapping->host);
  411. if (size < 0) {
  412. LINFO(curlun, "unable to find file size: %s\n", filename);
  413. rc = (int) size;
  414. goto out;
  415. }
  416. num_sectors = size >> 9; // File size in 512-byte blocks
  417. min_sectors = 1;
  418. if (curlun->cdrom) {
  419. num_sectors &= ~3; // Reduce to a multiple of 2048
  420. min_sectors = 300*4; // Smallest track is 300 frames
  421. if (num_sectors >= 256*60*75*4) {
  422. num_sectors = (256*60*75 - 1) * 4;
  423. LINFO(curlun, "file too big: %s\n", filename);
  424. LINFO(curlun, "using only first %d blocks\n",
  425. (int) num_sectors);
  426. }
  427. }
  428. if (num_sectors < min_sectors) {
  429. LINFO(curlun, "file too small: %s\n", filename);
  430. rc = -ETOOSMALL;
  431. goto out;
  432. }
  433. get_file(filp);
  434. curlun->ro = ro;
  435. curlun->filp = filp;
  436. curlun->file_length = size;
  437. curlun->num_sectors = num_sectors;
  438. LDBG(curlun, "open backing file: %s\n", filename);
  439. rc = 0;
  440. out:
  441. filp_close(filp, current->files);
  442. return rc;
  443. }
  444. static void fsg_lun_close(struct fsg_lun *curlun)
  445. {
  446. if (curlun->filp) {
  447. LDBG(curlun, "close backing file\n");
  448. fput(curlun->filp);
  449. curlun->filp = NULL;
  450. }
  451. }
  452. /*-------------------------------------------------------------------------*/
  453. /* Sync the file data, don't bother with the metadata.
  454. * This code was copied from fs/buffer.c:sys_fdatasync(). */
  455. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  456. {
  457. struct file *filp = curlun->filp;
  458. if (curlun->ro || !filp)
  459. return 0;
  460. return vfs_fsync(filp, filp->f_path.dentry, 1);
  461. }
  462. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  463. {
  464. if (msf) {
  465. /* Convert to Minutes-Seconds-Frames */
  466. addr >>= 2; /* Convert to 2048-byte frames */
  467. addr += 2*75; /* Lead-in occupies 2 seconds */
  468. dest[3] = addr % 75; /* Frames */
  469. addr /= 75;
  470. dest[2] = addr % 60; /* Seconds */
  471. addr /= 60;
  472. dest[1] = addr; /* Minutes */
  473. dest[0] = 0; /* Reserved */
  474. } else {
  475. /* Absolute sector */
  476. put_unaligned_be32(addr, dest);
  477. }
  478. }