blob: 3e2500f19140102a689d569a8aa5a2bc1c18066c [file] [log] [blame]
Michal Nazarewiczb6058d02009-10-28 16:57:14 +01001/*
2 * storage_common.c -- Common definitions for mass storage functionality
3 *
4 * Copyright (C) 2003-2008 Alan Stern
5 * Copyeight (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01006 * Author: Michal Nazarewicz (mina86@mina86.com)
Michal Nazarewiczd6181702009-10-28 16:57:15 +01007 *
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.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010012 */
13
Michal Nazarewiczd6181702009-10-28 16:57:15 +010014/*
15 * This file requires the following identifiers used in USB strings to
16 * be defined (each of type pointer to char):
Michal Nazarewiczd6181702009-10-28 16:57:15 +010017 * - fsg_string_interface -- name of the interface
Michal Nazarewicz93bcf122009-10-28 16:57:19 +010018 */
19
Michal Nazarewicz606206c22009-10-28 16:57:21 +010020/*
Per Forlin6532c7f2011-08-19 21:21:27 +020021 * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
22 * sets the number of pipeline buffers (length of the fsg_buffhd array).
23 * The valid range of num_buffers is: num >= 2 && num <= 4.
24 */
25
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020026#include <linux/module.h>
27#include <linux/blkdev.h>
28#include <linux/file.h>
29#include <linux/fs.h>
30#include <linux/usb/composite.h>
Michal Nazarewiczd6181702009-10-28 16:57:15 +010031
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020032#include "storage_common.h"
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010033
Michal Nazarewiczfea20db2012-11-06 22:52:39 +010034static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010035{
Michal Nazarewiczd6181702009-10-28 16:57:15 +010036 return container_of(dev, struct fsg_lun, dev);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010037}
38
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010039/* There is only one interface. */
40
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020041struct usb_interface_descriptor fsg_intf_desc = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +010042 .bLength = sizeof fsg_intf_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010043 .bDescriptorType = USB_DT_INTERFACE,
44
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010045 .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010046 .bInterfaceClass = USB_CLASS_MASS_STORAGE,
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +010047 .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
48 .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
Michal Nazarewiczd6181702009-10-28 16:57:15 +010049 .iInterface = FSG_STRING_INTERFACE,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010050};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020051EXPORT_SYMBOL(fsg_intf_desc);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010052
Michal Nazarewiczd0893262010-07-05 16:38:04 +020053/*
54 * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
55 * interrupt-in.
56 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010057
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020058struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010059 .bLength = USB_DT_ENDPOINT_SIZE,
60 .bDescriptorType = USB_DT_ENDPOINT,
61
62 .bEndpointAddress = USB_DIR_IN,
63 .bmAttributes = USB_ENDPOINT_XFER_BULK,
64 /* wMaxPacketSize set by autoconfiguration */
65};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020066EXPORT_SYMBOL(fsg_fs_bulk_in_desc);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010067
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020068struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010069 .bLength = USB_DT_ENDPOINT_SIZE,
70 .bDescriptorType = USB_DT_ENDPOINT,
71
72 .bEndpointAddress = USB_DIR_OUT,
73 .bmAttributes = USB_ENDPOINT_XFER_BULK,
74 /* wMaxPacketSize set by autoconfiguration */
75};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020076EXPORT_SYMBOL(fsg_fs_bulk_out_desc);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010077
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020078struct usb_descriptor_header *fsg_fs_function[] = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +010079 (struct usb_descriptor_header *) &fsg_intf_desc,
80 (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
81 (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010082 NULL,
83};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020084EXPORT_SYMBOL(fsg_fs_function);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010085
86
87/*
88 * USB 2.0 devices need to expose both high speed and full speed
89 * descriptors, unless they only run at full speed.
90 *
91 * That means alternate endpoint descriptors (bigger packets)
92 * and a "device qualifier" ... plus more construction options
Michal Nazarewiczd0893262010-07-05 16:38:04 +020093 * for the configuration descriptor.
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010094 */
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +020095struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +010096 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98
99 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
100 .bmAttributes = USB_ENDPOINT_XFER_BULK,
101 .wMaxPacketSize = cpu_to_le16(512),
102};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200103EXPORT_SYMBOL(fsg_hs_bulk_in_desc);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100104
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200105struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100106 .bLength = USB_DT_ENDPOINT_SIZE,
107 .bDescriptorType = USB_DT_ENDPOINT,
108
109 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
110 .bmAttributes = USB_ENDPOINT_XFER_BULK,
111 .wMaxPacketSize = cpu_to_le16(512),
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100112 .bInterval = 1, /* NAK every 1 uframe */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100113};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200114EXPORT_SYMBOL(fsg_hs_bulk_out_desc);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100115
Michal Nazarewicz93bcf122009-10-28 16:57:19 +0100116
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200117struct usb_descriptor_header *fsg_hs_function[] = {
Michal Nazarewiczd6181702009-10-28 16:57:15 +0100118 (struct usb_descriptor_header *) &fsg_intf_desc,
119 (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
120 (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100121 NULL,
122};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200123EXPORT_SYMBOL(fsg_hs_function);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100124
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200125struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300126 .bLength = USB_DT_ENDPOINT_SIZE,
127 .bDescriptorType = USB_DT_ENDPOINT,
128
129 /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
130 .bmAttributes = USB_ENDPOINT_XFER_BULK,
131 .wMaxPacketSize = cpu_to_le16(1024),
132};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200133EXPORT_SYMBOL(fsg_ss_bulk_in_desc);
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300134
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200135struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300136 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
137 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
138
139 /*.bMaxBurst = DYNAMIC, */
140};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200141EXPORT_SYMBOL(fsg_ss_bulk_in_comp_desc);
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300142
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200143struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300144 .bLength = USB_DT_ENDPOINT_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
146
147 /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
148 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 .wMaxPacketSize = cpu_to_le16(1024),
150};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200151EXPORT_SYMBOL(fsg_ss_bulk_out_desc);
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300152
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200153struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300154 .bLength = sizeof(fsg_ss_bulk_in_comp_desc),
155 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
156
157 /*.bMaxBurst = DYNAMIC, */
158};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200159EXPORT_SYMBOL(fsg_ss_bulk_out_comp_desc);
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300160
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200161struct usb_descriptor_header *fsg_ss_function[] = {
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300162 (struct usb_descriptor_header *) &fsg_intf_desc,
163 (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
164 (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
165 (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
166 (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300167 NULL,
168};
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200169EXPORT_SYMBOL(fsg_ss_function);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100170
171
172 /*-------------------------------------------------------------------------*/
173
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200174/*
175 * If the next two routines are called while the gadget is registered,
176 * the caller must own fsg->filesem for writing.
177 */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100178
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200179void fsg_lun_close(struct fsg_lun *curlun)
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200180{
181 if (curlun->filp) {
182 LDBG(curlun, "close backing file\n");
183 fput(curlun->filp);
184 curlun->filp = NULL;
185 }
186}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200187EXPORT_SYMBOL(fsg_lun_close);
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200188
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200189int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100190{
191 int ro;
192 struct file *filp = NULL;
193 int rc = -EINVAL;
194 struct inode *inode = NULL;
195 loff_t size;
196 loff_t num_sectors;
197 loff_t min_sectors;
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200198 unsigned int blkbits;
199 unsigned int blksize;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100200
201 /* R/W if we can, R/O if we must */
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100202 ro = curlun->initially_ro;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100203 if (!ro) {
204 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
Tejun Heo75f1dc02010-11-13 11:55:17 +0100205 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100206 ro = 1;
207 }
208 if (ro)
209 filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
210 if (IS_ERR(filp)) {
211 LINFO(curlun, "unable to open backing file: %s\n", filename);
212 return PTR_ERR(filp);
213 }
214
215 if (!(filp->f_mode & FMODE_WRITE))
216 ro = 1;
217
Al Viro496ad9a2013-01-23 17:07:38 -0500218 inode = file_inode(filp);
Al Viro20818a02012-07-22 21:23:33 +0400219 if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100220 LINFO(curlun, "invalid file type: %s\n", filename);
221 goto out;
222 }
223
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200224 /*
225 * If we can't read the file, it's no good.
226 * If we can't write the file, use it read-only.
227 */
Al Viro20818a02012-07-22 21:23:33 +0400228 if (!(filp->f_op->read || filp->f_op->aio_read)) {
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100229 LINFO(curlun, "file not readable: %s\n", filename);
230 goto out;
231 }
232 if (!(filp->f_op->write || filp->f_op->aio_write))
233 ro = 1;
234
235 size = i_size_read(inode->i_mapping->host);
236 if (size < 0) {
237 LINFO(curlun, "unable to find file size: %s\n", filename);
238 rc = (int) size;
239 goto out;
240 }
Peiyu Li3f565a32011-08-17 22:52:59 -0700241
242 if (curlun->cdrom) {
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200243 blksize = 2048;
244 blkbits = 11;
Peiyu Li3f565a32011-08-17 22:52:59 -0700245 } else if (inode->i_bdev) {
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200246 blksize = bdev_logical_block_size(inode->i_bdev);
247 blkbits = blksize_bits(blksize);
Peiyu Li3f565a32011-08-17 22:52:59 -0700248 } else {
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200249 blksize = 512;
250 blkbits = 9;
Peiyu Li3f565a32011-08-17 22:52:59 -0700251 }
252
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200253 num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100254 min_sectors = 1;
Michal Nazarewicze909ef52009-10-28 16:57:16 +0100255 if (curlun->cdrom) {
Peiyu Li3f565a32011-08-17 22:52:59 -0700256 min_sectors = 300; /* Smallest track is 300 frames */
257 if (num_sectors >= 256*60*75) {
258 num_sectors = 256*60*75 - 1;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100259 LINFO(curlun, "file too big: %s\n", filename);
260 LINFO(curlun, "using only first %d blocks\n",
261 (int) num_sectors);
262 }
263 }
264 if (num_sectors < min_sectors) {
265 LINFO(curlun, "file too small: %s\n", filename);
266 rc = -ETOOSMALL;
267 goto out;
268 }
269
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200270 if (fsg_lun_is_open(curlun))
271 fsg_lun_close(curlun);
272
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200273 curlun->blksize = blksize;
274 curlun->blkbits = blkbits;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100275 curlun->ro = ro;
276 curlun->filp = filp;
277 curlun->file_length = size;
278 curlun->num_sectors = num_sectors;
279 LDBG(curlun, "open backing file: %s\n", filename);
Al Viro20818a02012-07-22 21:23:33 +0400280 return 0;
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100281
282out:
Al Viro20818a02012-07-22 21:23:33 +0400283 fput(filp);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100284 return rc;
285}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200286EXPORT_SYMBOL(fsg_lun_open);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100287
288
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100289/*-------------------------------------------------------------------------*/
290
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200291/*
292 * Sync the file data, don't bother with the metadata.
293 * This code was copied from fs/buffer.c:sys_fdatasync().
294 */
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200295int fsg_lun_fsync_sub(struct fsg_lun *curlun)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100296{
297 struct file *filp = curlun->filp;
298
299 if (curlun->ro || !filp)
300 return 0;
Christoph Hellwig8018ab02010-03-22 17:32:25 +0100301 return vfs_fsync(filp, 1);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100302}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200303EXPORT_SYMBOL(fsg_lun_fsync_sub);
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100304
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200305void store_cdrom_address(u8 *dest, int msf, u32 addr)
Michal Nazarewiczb6058d02009-10-28 16:57:14 +0100306{
307 if (msf) {
308 /* Convert to Minutes-Seconds-Frames */
309 addr >>= 2; /* Convert to 2048-byte frames */
310 addr += 2*75; /* Lead-in occupies 2 seconds */
311 dest[3] = addr % 75; /* Frames */
312 addr /= 75;
313 dest[2] = addr % 60; /* Seconds */
314 addr /= 60;
315 dest[1] = addr; /* Minutes */
316 dest[0] = 0; /* Reserved */
317 } else {
318 /* Absolute sector */
319 put_unaligned_be32(addr, dest);
320 }
321}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200322EXPORT_SYMBOL(store_cdrom_address);
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100323
324/*-------------------------------------------------------------------------*/
325
326
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200327ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
328 char *buf)
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100329{
330 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
331
332 return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
333 ? curlun->ro
334 : curlun->initially_ro);
335}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200336EXPORT_SYMBOL(fsg_show_ro);
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100337
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200338ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
339 char *buf)
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300340{
341 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
342
343 return sprintf(buf, "%u\n", curlun->nofua);
344}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200345EXPORT_SYMBOL(fsg_show_nofua);
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300346
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200347ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
348 char *buf)
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100349{
350 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
351 struct rw_semaphore *filesem = dev_get_drvdata(dev);
352 char *p;
353 ssize_t rc;
354
355 down_read(filesem);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100356 if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100357 p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
358 if (IS_ERR(p))
359 rc = PTR_ERR(p);
360 else {
361 rc = strlen(p);
362 memmove(buf, p, rc);
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100363 buf[rc] = '\n'; /* Add a newline */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100364 buf[++rc] = 0;
365 }
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100366 } else { /* No file, return 0 bytes */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100367 *buf = 0;
368 rc = 0;
369 }
370 up_read(filesem);
371 return rc;
372}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200373EXPORT_SYMBOL(fsg_show_file);
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100374
375
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200376ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
377 const char *buf, size_t count)
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100378{
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200379 ssize_t rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100380 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
381 struct rw_semaphore *filesem = dev_get_drvdata(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200382 unsigned ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100383
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200384 rc = kstrtouint(buf, 2, &ro);
385 if (rc)
386 return rc;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100387
Michal Nazarewiczd0893262010-07-05 16:38:04 +0200388 /*
389 * Allow the write-enable status to change only while the
390 * backing file is closed.
391 */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100392 down_read(filesem);
393 if (fsg_lun_is_open(curlun)) {
394 LDBG(curlun, "read-only status change prevented\n");
395 rc = -EBUSY;
396 } else {
Andy Shevchenko8156d152010-07-22 11:58:47 +0300397 curlun->ro = ro;
398 curlun->initially_ro = ro;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100399 LDBG(curlun, "read-only status set to %d\n", curlun->ro);
Michal Nazarewiczfd4477b2011-04-14 11:55:43 +0200400 rc = count;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100401 }
402 up_read(filesem);
403 return rc;
404}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200405EXPORT_SYMBOL(fsg_store_ro);
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100406
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200407ssize_t fsg_store_nofua(struct device *dev, struct device_attribute *attr,
408 const char *buf, size_t count)
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300409{
410 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200411 unsigned nofua;
412 int ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300413
Michal Nazarewiczdb8fa282011-04-14 00:37:00 +0200414 ret = kstrtouint(buf, 2, &nofua);
415 if (ret)
416 return ret;
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300417
418 /* Sync data when switching from async mode to sync */
419 if (!nofua && curlun->nofua)
420 fsg_lun_fsync_sub(curlun);
421
422 curlun->nofua = nofua;
423
424 return count;
425}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200426EXPORT_SYMBOL(fsg_store_nofua);
Andy Shevchenkoa93917d2010-07-22 17:53:56 +0300427
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200428ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
429 const char *buf, size_t count)
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100430{
431 struct fsg_lun *curlun = fsg_lun_from_dev(dev);
432 struct rw_semaphore *filesem = dev_get_drvdata(dev);
433 int rc = 0;
434
435 if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
436 LDBG(curlun, "eject attempt prevented\n");
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100437 return -EBUSY; /* "Door is locked" */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100438 }
439
440 /* Remove a trailing newline */
441 if (count > 0 && buf[count-1] == '\n')
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +0100442 ((char *) buf)[count-1] = 0; /* Ugh! */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100443
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100444 /* Load new medium */
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200445 down_write(filesem);
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100446 if (count > 0 && buf[0]) {
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200447 /* fsg_lun_open() will close existing file if any. */
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100448 rc = fsg_lun_open(curlun, buf);
449 if (rc == 0)
450 curlun->unit_attention_data =
451 SS_NOT_READY_TO_READY_TRANSITION;
Michal Nazarewiczd6e16a82012-06-18 14:37:20 +0200452 } else if (fsg_lun_is_open(curlun)) {
453 fsg_lun_close(curlun);
454 curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
Michal Nazarewicz93f93742009-10-28 16:57:17 +0100455 }
456 up_write(filesem);
457 return (rc < 0 ? rc : count);
458}
Andrzej Pietrasiewicz6fdc5dd2013-09-26 14:38:16 +0200459EXPORT_SYMBOL(fsg_store_file);
460
461MODULE_LICENSE("GPL");