master/cdev.c
changeset 2105 86a4a7587c43
parent 2066 b544025bd696
child 2107 ebda087981e1
equal deleted inserted replaced
2103:8c63d804ead9 2105:86a4a7587c43
   856     ec_sdo_request_address(&request->req,
   856     ec_sdo_request_address(&request->req,
   857         data.sdo_index, data.sdo_entry_subindex);
   857         data.sdo_index, data.sdo_entry_subindex);
   858     ecrt_sdo_request_read(&request->req);
   858     ecrt_sdo_request_read(&request->req);
   859 
   859 
   860     if (ec_mutex_lock_interruptible(&master->master_mutex))  {
   860     if (ec_mutex_lock_interruptible(&master->master_mutex))  {
   861         kref_put(&request->refcount,ec_master_sdo_request_release);
   861         kref_put(&request->refcount, ec_master_sdo_request_release);
   862         return -EINTR;
   862         return -EINTR;
   863     }
   863     }
   864     if (!(request->slave = ec_master_find_slave(
   864     if (!(request->slave = ec_master_find_slave(
   865                     master, 0, data.slave_position))) {
   865                     master, 0, data.slave_position))) {
   866         ec_mutex_unlock(&master->master_mutex);
   866         ec_mutex_unlock(&master->master_mutex);
   867         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
   867         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
   868                 data.slave_position);
   868                 data.slave_position);
   869         kref_put(&request->refcount,ec_master_sdo_request_release);
   869         kref_put(&request->refcount, ec_master_sdo_request_release);
   870         return -EINVAL;
   870         return -EINVAL;
   871     }
   871     }
   872 
   872 
   873     EC_SLAVE_DBG(request->slave, 1, "Schedule SDO upload request %p.\n",request);
   873     EC_SLAVE_DBG(request->slave, 1, "Schedule SDO upload request %p.\n",
       
   874             request);
   874 
   875 
   875     // schedule request.
   876     // schedule request.
   876     kref_get(&request->refcount);
   877     kref_get(&request->refcount);
   877     list_add_tail(&request->list, &request->slave->slave_sdo_requests);
   878     list_add_tail(&request->list, &request->slave->slave_sdo_requests);
   878 
   879 
   879     ec_mutex_unlock(&master->master_mutex);
   880     ec_mutex_unlock(&master->master_mutex);
   880 
   881 
   881     // wait for processing through FSM
   882     // wait for processing through FSM
   882     if (wait_event_interruptible(request->slave->sdo_queue,
   883     if (wait_event_interruptible(request->slave->sdo_queue,
   883           ((request->req.state == EC_INT_REQUEST_SUCCESS) || (request->req.state == EC_INT_REQUEST_FAILURE)))) {
   884           ((request->req.state == EC_INT_REQUEST_SUCCESS) ||
       
   885            (request->req.state == EC_INT_REQUEST_FAILURE)))) {
   884         // interrupted by signal
   886         // interrupted by signal
   885         kref_put(&request->refcount,ec_master_sdo_request_release);
   887         kref_put(&request->refcount, ec_master_sdo_request_release);
   886         return -EINTR;
   888         return -EINTR;
   887     }
   889     }
   888 
   890 
   889     EC_SLAVE_DBG(request->slave, 1, "Finished SDO upload request %p.\n",request);
   891     EC_SLAVE_DBG(request->slave, 1, "Finished SDO upload request %p.\n",
       
   892             request);
   890 
   893 
   891     data.abort_code = request->req.abort_code;
   894     data.abort_code = request->req.abort_code;
   892 
   895 
   893     if (request->req.state != EC_INT_REQUEST_SUCCESS) {
   896     if (request->req.state != EC_INT_REQUEST_SUCCESS) {
   894         data.data_size = 0;
   897         data.data_size = 0;
   898             retval = -EIO;
   901             retval = -EIO;
   899         }
   902         }
   900     } else {
   903     } else {
   901         if (request->req.data_size > data.target_size) {
   904         if (request->req.data_size > data.target_size) {
   902             EC_MASTER_ERR(master, "Buffer too small.\n");
   905             EC_MASTER_ERR(master, "Buffer too small.\n");
   903             kref_put(&request->refcount,ec_master_sdo_request_release);
   906             kref_put(&request->refcount, ec_master_sdo_request_release);
   904             return -EOVERFLOW;
   907             return -EOVERFLOW;
   905         }
   908         }
   906         data.data_size = request->req.data_size;
   909         data.data_size = request->req.data_size;
   907 
   910 
   908         if (copy_to_user((void __user *) data.target,
   911         if (copy_to_user((void __user *) data.target,
   909                     request->req.data, data.data_size)) {
   912                     request->req.data, data.data_size)) {
   910             kref_put(&request->refcount,ec_master_sdo_request_release);
   913             kref_put(&request->refcount, ec_master_sdo_request_release);
   911             return -EFAULT;
   914             return -EFAULT;
   912         }
   915         }
   913         retval = 0;
   916         retval = 0;
   914     }
   917     }
   915 
   918 
   916     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
   919     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
   917         retval = -EFAULT;
   920         retval = -EFAULT;
   918     }
   921     }
   919 
   922 
   920     kref_put(&request->refcount,ec_master_sdo_request_release);
   923     kref_put(&request->refcount, ec_master_sdo_request_release);
   921     return retval;
   924     return retval;
   922 }
   925 }
   923 
   926 
   924 /*****************************************************************************/
   927 /*****************************************************************************/
   925 
   928 
   951 
   954 
   952     ec_sdo_request_init(&request->req);
   955     ec_sdo_request_init(&request->req);
   953     ec_sdo_request_address(&request->req,
   956     ec_sdo_request_address(&request->req,
   954             data.sdo_index, data.sdo_entry_subindex);
   957             data.sdo_index, data.sdo_entry_subindex);
   955     if (ec_sdo_request_alloc(&request->req, data.data_size)) {
   958     if (ec_sdo_request_alloc(&request->req, data.data_size)) {
   956         kref_put(&request->refcount,ec_master_sdo_request_release);
   959         kref_put(&request->refcount, ec_master_sdo_request_release);
   957         return -ENOMEM;
   960         return -ENOMEM;
   958     }
   961     }
   959     if (copy_from_user(request->req.data,
   962     if (copy_from_user(request->req.data,
   960                 (void __user *) data.data, data.data_size)) {
   963                 (void __user *) data.data, data.data_size)) {
   961         kref_put(&request->refcount,ec_master_sdo_request_release);
   964         kref_put(&request->refcount, ec_master_sdo_request_release);
   962         return -EFAULT;
   965         return -EFAULT;
   963     }
   966     }
   964     request->req.data_size = data.data_size;
   967     request->req.data_size = data.data_size;
   965     ecrt_sdo_request_write(&request->req);
   968     ecrt_sdo_request_write(&request->req);
   966 
   969 
   967     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
   970     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
   968         kref_put(&request->refcount,ec_master_sdo_request_release);
   971         kref_put(&request->refcount, ec_master_sdo_request_release);
   969         return -EINTR;
   972         return -EINTR;
   970     }
   973     }
   971     if (!(request->slave = ec_master_find_slave(
   974     if (!(request->slave = ec_master_find_slave(
   972                     master, 0, data.slave_position))) {
   975                     master, 0, data.slave_position))) {
   973         ec_mutex_unlock(&master->master_mutex);
   976         ec_mutex_unlock(&master->master_mutex);
   974         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
   977         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
   975                 data.slave_position);
   978                 data.slave_position);
   976         kref_put(&request->refcount,ec_master_sdo_request_release);
   979         kref_put(&request->refcount, ec_master_sdo_request_release);
   977         return -EINVAL;
   980         return -EINVAL;
   978     }
   981     }
   979     
   982     
   980     EC_SLAVE_DBG(request->slave, 1, "Schedule SDO download request %p.\n",request);
   983     EC_SLAVE_DBG(request->slave, 1, "Schedule SDO download request %p.\n",
       
   984             request);
   981 
   985 
   982     // schedule request.
   986     // schedule request.
   983     kref_get(&request->refcount);
   987     kref_get(&request->refcount);
   984     list_add_tail(&request->list, &request->slave->slave_sdo_requests);
   988     list_add_tail(&request->list, &request->slave->slave_sdo_requests);
   985 
   989 
   986     ec_mutex_unlock(&master->master_mutex);
   990     ec_mutex_unlock(&master->master_mutex);
   987 
   991 
   988     // wait for processing through FSM
   992     // wait for processing through FSM
   989     if (wait_event_interruptible(request->slave->sdo_queue,
   993     if (wait_event_interruptible(request->slave->sdo_queue,
   990        ((request->req.state == EC_INT_REQUEST_SUCCESS) || (request->req.state == EC_INT_REQUEST_FAILURE)))) {
   994        ((request->req.state == EC_INT_REQUEST_SUCCESS) ||
       
   995         (request->req.state == EC_INT_REQUEST_FAILURE)))) {
   991         // interrupted by signal
   996         // interrupted by signal
   992         kref_put(&request->refcount,ec_master_sdo_request_release);
   997         kref_put(&request->refcount, ec_master_sdo_request_release);
   993         return -EINTR;
   998         return -EINTR;
   994     }
   999     }
   995 
  1000 
   996     EC_SLAVE_DBG(request->slave, 1, "Finished SDO download request %p.\n",request);
  1001     EC_SLAVE_DBG(request->slave, 1, "Finished SDO download request %p.\n",
       
  1002             request);
   997 
  1003 
   998     data.abort_code = request->req.abort_code;
  1004     data.abort_code = request->req.abort_code;
   999 
  1005 
  1000     if (request->req.state == EC_INT_REQUEST_SUCCESS) {
  1006     if (request->req.state == EC_INT_REQUEST_SUCCESS) {
  1001         retval = 0;
  1007         retval = 0;
  1007 
  1013 
  1008     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  1014     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  1009         retval = -EFAULT;
  1015         retval = -EFAULT;
  1010     }
  1016     }
  1011 
  1017 
  1012     kref_put(&request->refcount,ec_master_sdo_request_release);
  1018     kref_put(&request->refcount, ec_master_sdo_request_release);
  1013     return retval;
  1019     return retval;
  1014 }
  1020 }
  1015 
  1021 
  1016 /*****************************************************************************/
  1022 /*****************************************************************************/
  1017 
  1023 
  1091 
  1097 
  1092     request = kmalloc(sizeof(*request), GFP_KERNEL);
  1098     request = kmalloc(sizeof(*request), GFP_KERNEL);
  1093     if (!request)
  1099     if (!request)
  1094         return -ENOMEM;
  1100         return -ENOMEM;
  1095     kref_init(&request->refcount);
  1101     kref_init(&request->refcount);
       
  1102 
  1096     // init SII write request
  1103     // init SII write request
  1097     INIT_LIST_HEAD(&request->list);
  1104     INIT_LIST_HEAD(&request->list);
  1098     request->words = words; // now "owned" by request, see ec_master_sii_write_request_release
  1105     request->words = words; /* now "owned" by request, see
       
  1106                              * ec_master_sii_write_request_release */
  1099     request->offset = data.offset;
  1107     request->offset = data.offset;
  1100     request->nwords = data.nwords;
  1108     request->nwords = data.nwords;
  1101 
  1109 
  1102     if (copy_from_user(words,
  1110     if (copy_from_user(words,
  1103                 (void __user *) data.words, byte_size)) {
  1111                 (void __user *) data.words, byte_size)) {
  1104         kref_put(&request->refcount,ec_master_sii_write_request_release);
  1112         kref_put(&request->refcount, ec_master_sii_write_request_release);
  1105         return -EFAULT;
  1113         return -EFAULT;
  1106     }
  1114     }
  1107 
  1115 
  1108     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1116     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1109         kref_put(&request->refcount,ec_master_sii_write_request_release);
  1117         kref_put(&request->refcount, ec_master_sii_write_request_release);
  1110         return -EINTR;
  1118         return -EINTR;
  1111     }
  1119     }
  1112     if (!(slave = ec_master_find_slave(
  1120     if (!(slave = ec_master_find_slave(
  1113                     master, 0, data.slave_position))) {
  1121                     master, 0, data.slave_position))) {
  1114         ec_mutex_unlock(&master->master_mutex);
  1122         ec_mutex_unlock(&master->master_mutex);
  1115         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1123         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1116                 data.slave_position);
  1124                 data.slave_position);
  1117         kref_put(&request->refcount,ec_master_sii_write_request_release);
  1125         kref_put(&request->refcount, ec_master_sii_write_request_release);
  1118         return -EINVAL;
  1126         return -EINVAL;
  1119     }
  1127     }
  1120 
  1128 
  1121     request->slave = slave;
  1129     request->slave = slave;
  1122     request->state = EC_INT_REQUEST_QUEUED;
  1130     request->state = EC_INT_REQUEST_QUEUED;
  1127 
  1135 
  1128     ec_mutex_unlock(&master->master_mutex);
  1136     ec_mutex_unlock(&master->master_mutex);
  1129 
  1137 
  1130     // wait for processing through FSM
  1138     // wait for processing through FSM
  1131     if (wait_event_interruptible(master->sii_queue,
  1139     if (wait_event_interruptible(master->sii_queue,
  1132           ((request->state == EC_INT_REQUEST_SUCCESS) || (request->state == EC_INT_REQUEST_FAILURE)))) {
  1140           ((request->state == EC_INT_REQUEST_SUCCESS) ||
       
  1141            (request->state == EC_INT_REQUEST_FAILURE)))) {
  1133            // interrupted by signal
  1142            // interrupted by signal
  1134            kref_put(&request->refcount,ec_master_sii_write_request_release);
  1143            kref_put(&request->refcount, ec_master_sii_write_request_release);
  1135            return -EINTR;
  1144            return -EINTR;
  1136     }
  1145     }
  1137 
  1146 
  1138 
       
  1139     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1147     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1140     kref_put(&request->refcount,ec_master_sii_write_request_release);
  1148     kref_put(&request->refcount, ec_master_sii_write_request_release);
  1141 
  1149 
  1142     return retval;
  1150     return retval;
  1143 }
  1151 }
  1144 
  1152 
  1145 /*****************************************************************************/
  1153 /*****************************************************************************/
  1176     kref_init(&request->refcount);
  1184     kref_init(&request->refcount);
  1177 
  1185 
  1178     // init register request
  1186     // init register request
  1179     INIT_LIST_HEAD(&request->list);
  1187     INIT_LIST_HEAD(&request->list);
  1180     request->dir = EC_DIR_INPUT;
  1188     request->dir = EC_DIR_INPUT;
  1181     request->data = contents;   // now "owned" by request, see ec_master_reg_request_release
  1189     request->data = contents; /* now "owned" by request, see
       
  1190                                * ec_master_reg_request_release */
  1182     request->offset = data.offset;
  1191     request->offset = data.offset;
  1183     request->length = data.length;
  1192     request->length = data.length;
  1184 
  1193 
  1185     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1194     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1186         kref_put(&request->refcount,ec_master_reg_request_release);
  1195         kref_put(&request->refcount, ec_master_reg_request_release);
  1187         return -EINTR;
  1196         return -EINTR;
  1188     }
  1197     }
  1189     if (!(slave = ec_master_find_slave(
  1198     if (!(slave = ec_master_find_slave(
  1190                     master, 0, data.slave_position))) {
  1199                     master, 0, data.slave_position))) {
  1191         ec_mutex_unlock(&master->master_mutex);
  1200         ec_mutex_unlock(&master->master_mutex);
  1192         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1201         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1193                 data.slave_position);
  1202                 data.slave_position);
  1194         kref_put(&request->refcount,ec_master_reg_request_release);
  1203         kref_put(&request->refcount, ec_master_reg_request_release);
  1195         return -EINVAL;
  1204         return -EINVAL;
  1196     }
  1205     }
  1197 
  1206 
  1198     request->slave = slave;
  1207     request->slave = slave;
  1199     request->state = EC_INT_REQUEST_QUEUED;
  1208     request->state = EC_INT_REQUEST_QUEUED;
  1204 
  1213 
  1205     ec_mutex_unlock(&master->master_mutex);
  1214     ec_mutex_unlock(&master->master_mutex);
  1206 
  1215 
  1207     // wait for processing through FSM
  1216     // wait for processing through FSM
  1208     if (wait_event_interruptible(master->reg_queue,
  1217     if (wait_event_interruptible(master->reg_queue,
  1209           ((request->state == EC_INT_REQUEST_SUCCESS) || (request->state == EC_INT_REQUEST_FAILURE)))) {
  1218           ((request->state == EC_INT_REQUEST_SUCCESS) ||
       
  1219            (request->state == EC_INT_REQUEST_FAILURE)))) {
  1210            // interrupted by signal
  1220            // interrupted by signal
  1211            kref_put(&request->refcount,ec_master_reg_request_release);
  1221            kref_put(&request->refcount, ec_master_reg_request_release);
  1212            return -EINTR;
  1222            return -EINTR;
  1213     }
  1223     }
  1214 
  1224 
  1215     if (request->state == EC_INT_REQUEST_SUCCESS) {
  1225     if (request->state == EC_INT_REQUEST_SUCCESS) {
  1216         if (copy_to_user((void __user *) data.data, request->data, data.length)) {
  1226         if (copy_to_user((void __user *) data.data, request->data,
  1217             kref_put(&request->refcount,ec_master_reg_request_release);
  1227                     data.length)) {
       
  1228             kref_put(&request->refcount, ec_master_reg_request_release);
  1218             return -EFAULT;
  1229             return -EFAULT;
  1219         }
  1230         }
  1220     }
  1231     }
  1221     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1232     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1222 
  1233 
  1223     kref_put(&request->refcount,ec_master_reg_request_release);
  1234     kref_put(&request->refcount, ec_master_reg_request_release);
  1224     return retval;
  1235     return retval;
  1225 }
  1236 }
  1226 
  1237 
  1227 /*****************************************************************************/
  1238 /*****************************************************************************/
  1228 
  1239 
  1262         return -ENOMEM;
  1273         return -ENOMEM;
  1263     kref_init(&request->refcount);
  1274     kref_init(&request->refcount);
  1264     // init register request
  1275     // init register request
  1265     INIT_LIST_HEAD(&request->list);
  1276     INIT_LIST_HEAD(&request->list);
  1266     request->dir = EC_DIR_OUTPUT;
  1277     request->dir = EC_DIR_OUTPUT;
  1267     request->data = contents; // now "owned" by request, see ec_master_reg_request_release
  1278     request->data = contents; /* now "owned" by request, see
       
  1279                                * ec_master_reg_request_release */
  1268     request->offset = data.offset;
  1280     request->offset = data.offset;
  1269     request->length = data.length;
  1281     request->length = data.length;
  1270 
  1282 
  1271     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1283     if (ec_mutex_lock_interruptible(&master->master_mutex)) {
  1272         kref_put(&request->refcount,ec_master_reg_request_release);
  1284         kref_put(&request->refcount, ec_master_reg_request_release);
  1273         return -EINTR;
  1285         return -EINTR;
  1274     }
  1286     }
  1275 
  1287 
  1276     if (!(slave = ec_master_find_slave(
  1288     if (!(slave = ec_master_find_slave(
  1277                     master, 0, data.slave_position))) {
  1289                     master, 0, data.slave_position))) {
  1278         ec_mutex_unlock(&master->master_mutex);
  1290         ec_mutex_unlock(&master->master_mutex);
  1279         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1291         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  1280                 data.slave_position);
  1292                 data.slave_position);
  1281         kref_put(&request->refcount,ec_master_reg_request_release);
  1293         kref_put(&request->refcount, ec_master_reg_request_release);
  1282         return -EINVAL;
  1294         return -EINVAL;
  1283     }
  1295     }
  1284 
  1296 
  1285     request->slave = slave;
  1297     request->slave = slave;
  1286     request->state = EC_INT_REQUEST_QUEUED;
  1298     request->state = EC_INT_REQUEST_QUEUED;
  1291 
  1303 
  1292     ec_mutex_unlock(&master->master_mutex);
  1304     ec_mutex_unlock(&master->master_mutex);
  1293 
  1305 
  1294     // wait for processing through FSM
  1306     // wait for processing through FSM
  1295     if (wait_event_interruptible(master->reg_queue,
  1307     if (wait_event_interruptible(master->reg_queue,
  1296           ((request->state == EC_INT_REQUEST_SUCCESS) || (request->state == EC_INT_REQUEST_FAILURE)))) {
  1308           ((request->state == EC_INT_REQUEST_SUCCESS) ||
       
  1309            (request->state == EC_INT_REQUEST_FAILURE)))) {
  1297            // interrupted by signal
  1310            // interrupted by signal
  1298            kref_put(&request->refcount,ec_master_reg_request_release);
  1311            kref_put(&request->refcount, ec_master_reg_request_release);
  1299            return -EINTR;
  1312            return -EINTR;
  1300     }
  1313     }
  1301 
  1314 
  1302     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1315     retval = request->state == EC_INT_REQUEST_SUCCESS ? 0 : -EIO;
  1303     kref_put(&request->refcount,ec_master_reg_request_release);
  1316     kref_put(&request->refcount, ec_master_reg_request_release);
  1304     return retval;
  1317     return retval;
  1305 
       
  1306 }
  1318 }
  1307 
  1319 
  1308 /*****************************************************************************/
  1320 /*****************************************************************************/
  1309 
  1321 
  1310 /** Get slave configuration information.
  1322 /** Get slave configuration information.
  1809         return -EFAULT;
  1821         return -EFAULT;
  1810     }
  1822     }
  1811 
  1823 
  1812     if (ec_mutex_lock_interruptible(&master->master_mutex))
  1824     if (ec_mutex_lock_interruptible(&master->master_mutex))
  1813         return -EINTR;
  1825         return -EINTR;
  1814     ec_master_set_send_interval(master,send_interval);
  1826     ec_master_set_send_interval(master, send_interval);
  1815     ec_mutex_unlock(&master->master_mutex);
  1827     ec_mutex_unlock(&master->master_mutex);
  1816 
  1828 
  1817     return 0;
  1829     return 0;
  1818 }
  1830 }
  1819 
  1831 
  3307     ec_foe_request_init(&request->req, data.file_name);
  3319     ec_foe_request_init(&request->req, data.file_name);
  3308     ec_foe_request_read(&request->req);
  3320     ec_foe_request_read(&request->req);
  3309     ec_foe_request_alloc(&request->req, 10000); // FIXME
  3321     ec_foe_request_alloc(&request->req, 10000); // FIXME
  3310 
  3322 
  3311     if (ec_mutex_lock_interruptible(&master->master_mutex))  {
  3323     if (ec_mutex_lock_interruptible(&master->master_mutex))  {
  3312         kref_put(&request->refcount,ec_master_foe_request_release);
  3324         kref_put(&request->refcount, ec_master_foe_request_release);
  3313         return -EINTR;
  3325         return -EINTR;
  3314     }
  3326     }
  3315     if (!(request->slave = ec_master_find_slave(
  3327     if (!(request->slave = ec_master_find_slave(
  3316                     master, 0, data.slave_position))) {
  3328                     master, 0, data.slave_position))) {
  3317         ec_mutex_unlock(&master->master_mutex);
  3329         ec_mutex_unlock(&master->master_mutex);
  3318         kref_put(&request->refcount,ec_master_foe_request_release);
  3330         kref_put(&request->refcount, ec_master_foe_request_release);
  3319         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  3331         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  3320                 data.slave_position);
  3332                 data.slave_position);
  3321         return -EINVAL;
  3333         return -EINVAL;
  3322     }
  3334     }
  3323 
  3335 
  3325     list_add_tail(&request->list, &request->slave->foe_requests);
  3337     list_add_tail(&request->list, &request->slave->foe_requests);
  3326     kref_get(&request->refcount);
  3338     kref_get(&request->refcount);
  3327 
  3339 
  3328     ec_mutex_unlock(&master->master_mutex);
  3340     ec_mutex_unlock(&master->master_mutex);
  3329 
  3341 
  3330     EC_SLAVE_DBG(request->slave, 1, "Scheduled FoE read request %p.\n",request);
  3342     EC_SLAVE_DBG(request->slave, 1, "Scheduled FoE read request %p.\n",
       
  3343             request);
  3331 
  3344 
  3332     // wait for processing through FSM
  3345     // wait for processing through FSM
  3333     if (wait_event_interruptible(request->slave->foe_queue,
  3346     if (wait_event_interruptible(request->slave->foe_queue,
  3334           ((request->req.state == EC_INT_REQUEST_SUCCESS) || (request->req.state == EC_INT_REQUEST_FAILURE)))) {
  3347           ((request->req.state == EC_INT_REQUEST_SUCCESS) ||
       
  3348            (request->req.state == EC_INT_REQUEST_FAILURE)))) {
  3335         // interrupted by signal
  3349         // interrupted by signal
  3336         kref_put(&request->refcount,ec_master_foe_request_release);
  3350         kref_put(&request->refcount, ec_master_foe_request_release);
  3337         return -EINTR;
  3351         return -EINTR;
  3338     }
  3352     }
  3339 
  3353 
  3340     data.result = request->req.result;
  3354     data.result = request->req.result;
  3341     data.error_code = request->req.error_code;
  3355     data.error_code = request->req.error_code;
  3342 
  3356 
  3343     EC_SLAVE_DBG(request->slave, 1, "Read %zd bytes via FoE"
  3357     EC_SLAVE_DBG(request->slave, 1, "Read %zd bytes via FoE"
  3344             " (result = 0x%x).\n", request->req.data_size, request->req.result);
  3358             " (result = 0x%x).\n", request->req.data_size,
       
  3359             request->req.result);
  3345 
  3360 
  3346     if (request->req.state != EC_INT_REQUEST_SUCCESS) {
  3361     if (request->req.state != EC_INT_REQUEST_SUCCESS) {
  3347         data.data_size = 0;
  3362         data.data_size = 0;
  3348         retval = -EIO;
  3363         retval = -EIO;
  3349     } else {
  3364     } else {
  3350         if (request->req.data_size > data.buffer_size) {
  3365         if (request->req.data_size > data.buffer_size) {
  3351             EC_MASTER_ERR(master, "Buffer too small.\n");
  3366             EC_MASTER_ERR(master, "Buffer too small.\n");
  3352             kref_put(&request->refcount,ec_master_foe_request_release);
  3367             kref_put(&request->refcount, ec_master_foe_request_release);
  3353             return -EOVERFLOW;
  3368             return -EOVERFLOW;
  3354         }
  3369         }
  3355         data.data_size = request->req.data_size;
  3370         data.data_size = request->req.data_size;
  3356         if (copy_to_user((void __user *) data.buffer,
  3371         if (copy_to_user((void __user *) data.buffer,
  3357                     request->req.buffer, data.data_size)) {
  3372                     request->req.buffer, data.data_size)) {
  3358             kref_put(&request->refcount,ec_master_foe_request_release);
  3373             kref_put(&request->refcount, ec_master_foe_request_release);
  3359             return -EFAULT;
  3374             return -EFAULT;
  3360         }
  3375         }
  3361         retval = 0;
  3376         retval = 0;
  3362     }
  3377     }
  3363 
  3378 
  3364     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  3379     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  3365         retval = -EFAULT;
  3380         retval = -EFAULT;
  3366     }
  3381     }
  3367 
  3382 
  3368     EC_SLAVE_DBG(request->slave, 1, "Finished FoE read request %p.\n",request);
  3383     EC_SLAVE_DBG(request->slave, 1, "Finished FoE read request %p.\n",
  3369     kref_put(&request->refcount,ec_master_foe_request_release);
  3384             request);
       
  3385     kref_put(&request->refcount, ec_master_foe_request_release);
  3370 
  3386 
  3371     return retval;
  3387     return retval;
  3372 }
  3388 }
  3373 
  3389 
  3374 /*****************************************************************************/
  3390 /*****************************************************************************/
  3396     INIT_LIST_HEAD(&request->list);
  3412     INIT_LIST_HEAD(&request->list);
  3397 
  3413 
  3398     ec_foe_request_init(&request->req, data.file_name);
  3414     ec_foe_request_init(&request->req, data.file_name);
  3399 
  3415 
  3400     if (ec_foe_request_alloc(&request->req, data.buffer_size)) {
  3416     if (ec_foe_request_alloc(&request->req, data.buffer_size)) {
  3401         kref_put(&request->refcount,ec_master_foe_request_release);
  3417         kref_put(&request->refcount, ec_master_foe_request_release);
  3402         return -ENOMEM;
  3418         return -ENOMEM;
  3403     }
  3419     }
  3404     if (copy_from_user(request->req.buffer,
  3420     if (copy_from_user(request->req.buffer,
  3405                 (void __user *) data.buffer, data.buffer_size)) {
  3421                 (void __user *) data.buffer, data.buffer_size)) {
  3406         kref_put(&request->refcount,ec_master_foe_request_release);
  3422         kref_put(&request->refcount, ec_master_foe_request_release);
  3407         return -EFAULT;
  3423         return -EFAULT;
  3408     }
  3424     }
  3409     request->req.data_size = data.buffer_size;
  3425     request->req.data_size = data.buffer_size;
  3410     ec_foe_request_write(&request->req);
  3426     ec_foe_request_write(&request->req);
  3411 
  3427 
  3415     if (!(request->slave = ec_master_find_slave(
  3431     if (!(request->slave = ec_master_find_slave(
  3416                     master, 0, data.slave_position))) {
  3432                     master, 0, data.slave_position))) {
  3417         ec_mutex_unlock(&master->master_mutex);
  3433         ec_mutex_unlock(&master->master_mutex);
  3418         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  3434         EC_MASTER_ERR(master, "Slave %u does not exist!\n",
  3419                 data.slave_position);
  3435                 data.slave_position);
  3420         kref_put(&request->refcount,ec_master_foe_request_release);
  3436         kref_put(&request->refcount, ec_master_foe_request_release);
  3421         return -EINVAL;
  3437         return -EINVAL;
  3422     }
  3438     }
  3423 
  3439 
  3424     EC_SLAVE_DBG(request->slave, 1, "Scheduling FoE write request %p.\n",request);
  3440     EC_SLAVE_DBG(request->slave, 1, "Scheduling FoE write request %p.\n",
       
  3441             request);
  3425 
  3442 
  3426     // schedule FoE write request.
  3443     // schedule FoE write request.
  3427     list_add_tail(&request->list, &request->slave->foe_requests);
  3444     list_add_tail(&request->list, &request->slave->foe_requests);
  3428     kref_get(&request->refcount);
  3445     kref_get(&request->refcount);
  3429 
  3446 
  3430     ec_mutex_unlock(&master->master_mutex);
  3447     ec_mutex_unlock(&master->master_mutex);
  3431 
  3448 
  3432     // wait for processing through FSM
  3449     // wait for processing through FSM
  3433     if (wait_event_interruptible(request->slave->foe_queue,
  3450     if (wait_event_interruptible(request->slave->foe_queue,
  3434        ((request->req.state == EC_INT_REQUEST_SUCCESS) || (request->req.state == EC_INT_REQUEST_FAILURE)))) {
  3451        ((request->req.state == EC_INT_REQUEST_SUCCESS) ||
       
  3452         (request->req.state == EC_INT_REQUEST_FAILURE)))) {
  3435         // interrupted by signal
  3453         // interrupted by signal
  3436         kref_put(&request->refcount,ec_master_foe_request_release);
  3454         kref_put(&request->refcount, ec_master_foe_request_release);
  3437         return -EINTR;
  3455         return -EINTR;
  3438     }
  3456     }
  3439 
  3457 
  3440     data.result = request->req.result;
  3458     data.result = request->req.result;
  3441     data.error_code = request->req.error_code;
  3459     data.error_code = request->req.error_code;
  3444 
  3462 
  3445     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  3463     if (__copy_to_user((void __user *) arg, &data, sizeof(data))) {
  3446         retval = -EFAULT;
  3464         retval = -EFAULT;
  3447     }
  3465     }
  3448 
  3466 
  3449     EC_SLAVE_DBG(request->slave, 1, "Finished FoE write request %p.\n",request);
  3467     EC_SLAVE_DBG(request->slave, 1, "Finished FoE write request %p.\n",
  3450     kref_put(&request->refcount,ec_master_foe_request_release);
  3468             request);
       
  3469     kref_put(&request->refcount, ec_master_foe_request_release);
  3451 
  3470 
  3452     return retval;
  3471     return retval;
  3453 }
  3472 }
  3454 
  3473 
  3455 /*****************************************************************************/
  3474 /*****************************************************************************/
  3482     if (retval) {
  3501     if (retval) {
  3483         kfree(data);
  3502         kfree(data);
  3484         return retval;
  3503         return retval;
  3485     }
  3504     }
  3486 
  3505 
  3487     if (copy_to_user((void __user *) ioctl.data,
  3506     if (copy_to_user((void __user *) ioctl.data, data, ioctl.data_size)) {
  3488                 data, ioctl.data_size)) {
       
  3489         kfree(data);
  3507         kfree(data);
  3490         return -EFAULT;
  3508         return -EFAULT;
  3491     }
  3509     }
  3492     kfree(data);
  3510     kfree(data);
  3493 
  3511 
  3755                 return -EPERM;
  3773                 return -EPERM;
  3756             return ec_cdev_ioctl_sc_watchdog(master, arg, priv);
  3774             return ec_cdev_ioctl_sc_watchdog(master, arg, priv);
  3757         case EC_IOCTL_SC_OVERLAPPING_IO:
  3775         case EC_IOCTL_SC_OVERLAPPING_IO:
  3758             if (!(filp->f_mode & FMODE_WRITE))
  3776             if (!(filp->f_mode & FMODE_WRITE))
  3759                 return -EPERM;
  3777                 return -EPERM;
  3760             return ec_cdev_ioctl_sc_allow_overlapping_pdos(master,arg,priv);
  3778             return ec_cdev_ioctl_sc_allow_overlapping_pdos(master, arg, priv);
  3761         case EC_IOCTL_SC_ADD_PDO:
  3779         case EC_IOCTL_SC_ADD_PDO:
  3762             if (!(filp->f_mode & FMODE_WRITE))
  3780             if (!(filp->f_mode & FMODE_WRITE))
  3763                 return -EPERM;
  3781                 return -EPERM;
  3764             return ec_cdev_ioctl_sc_add_pdo(master, arg, priv);
  3782             return ec_cdev_ioctl_sc_add_pdo(master, arg, priv);
  3765         case EC_IOCTL_SC_CLEAR_PDOS:
  3783         case EC_IOCTL_SC_CLEAR_PDOS: