@@ -172,7 +172,7 @@ static const int debug;
172172 * run out past 500ms.
173173 */
174174
175- static int wait_status (struct applesmc_device * smc , u8 val , u8 mask )
175+ static int port_wait_status (struct applesmc_device * smc , u8 val , u8 mask )
176176{
177177 u8 status ;
178178 int us ;
@@ -190,13 +190,13 @@ static int wait_status(struct applesmc_device *smc, u8 val, u8 mask)
190190 return - EIO ;
191191}
192192
193- /* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
193+ /* port_send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
194194
195- static int send_byte (struct applesmc_device * smc , u8 cmd , u16 port )
195+ static int port_send_byte (struct applesmc_device * smc , u8 cmd , u16 port )
196196{
197197 int status ;
198198
199- status = wait_status (smc , 0 , SMC_STATUS_IB_CLOSED );
199+ status = port_wait_status (smc , 0 , SMC_STATUS_IB_CLOSED );
200200 if (status )
201201 return status ;
202202 /*
@@ -205,23 +205,24 @@ static int send_byte(struct applesmc_device *smc, u8 cmd, u16 port)
205205 * this extra read may not happen if status returns both
206206 * simultaneously and this would appear to be required.
207207 */
208- status = wait_status (smc , SMC_STATUS_BUSY , SMC_STATUS_BUSY );
208+ status = port_wait_status (smc , SMC_STATUS_BUSY , SMC_STATUS_BUSY );
209209 if (status )
210210 return status ;
211211
212212 outb (cmd , smc -> port_base + port );
213213 return 0 ;
214214}
215215
216- /* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
216+ /* port_send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
217217
218- static int send_command (struct applesmc_device * smc , u8 cmd )
218+ static int port_send_command (struct applesmc_device * smc , u8 cmd )
219219{
220220 int ret ;
221221
222- ret = wait_status (smc , 0 , SMC_STATUS_IB_CLOSED );
222+ ret = port_wait_status (smc , 0 , SMC_STATUS_IB_CLOSED );
223223 if (ret )
224224 return ret ;
225+
225226 outb (cmd , smc -> port_base + APPLESMC_CMD_PORT );
226227 return 0 ;
227228}
@@ -232,53 +233,53 @@ static int send_command(struct applesmc_device *smc, u8 cmd)
232233 * If busy is stuck high after the command then the SMC is jammed.
233234 */
234235
235- static int smc_sane (struct applesmc_device * smc )
236+ static int port_smc_sane (struct applesmc_device * smc )
236237{
237238 int ret ;
238239
239- ret = wait_status (smc , 0 , SMC_STATUS_BUSY );
240+ ret = port_wait_status (smc , 0 , SMC_STATUS_BUSY );
240241 if (!ret )
241242 return ret ;
242- ret = send_command (smc , APPLESMC_READ_CMD );
243+ ret = port_send_command (smc , APPLESMC_READ_CMD );
243244 if (ret )
244245 return ret ;
245- return wait_status (smc , 0 , SMC_STATUS_BUSY );
246+ return port_wait_status (smc , 0 , SMC_STATUS_BUSY );
246247}
247248
248- static int send_argument (struct applesmc_device * smc , const char * key )
249+ static int port_send_argument (struct applesmc_device * smc , const char * key )
249250{
250251 int i ;
251252
252253 for (i = 0 ; i < 4 ; i ++ )
253- if (send_byte (smc , key [i ], APPLESMC_DATA_PORT ))
254+ if (port_send_byte (smc , key [i ], APPLESMC_DATA_PORT ))
254255 return - EIO ;
255256 return 0 ;
256257}
257258
258- static int read_smc (struct applesmc_device * smc , u8 cmd , const char * key ,
259+ static int port_read_smc (struct applesmc_device * smc , u8 cmd , const char * key ,
259260 u8 * buffer , u8 len )
260261{
261262 u8 status , data = 0 ;
262263 int i ;
263264 int ret ;
264265
265- ret = smc_sane (smc );
266+ ret = port_smc_sane (smc );
266267 if (ret )
267268 return ret ;
268269
269- if (send_command (smc , cmd ) || send_argument (smc , key )) {
270+ if (port_send_command (smc , cmd ) || port_send_argument (smc , key )) {
270271 pr_warn ("%.4s: read arg fail\n" , key );
271272 return - EIO ;
272273 }
273274
274275 /* This has no effect on newer (2012) SMCs */
275- if (send_byte (smc , len , APPLESMC_DATA_PORT )) {
276+ if (port_send_byte (smc , len , APPLESMC_DATA_PORT )) {
276277 pr_warn ("%.4s: read len fail\n" , key );
277278 return - EIO ;
278279 }
279280
280281 for (i = 0 ; i < len ; i ++ ) {
281- if (wait_status (smc ,
282+ if (port_wait_status (smc ,
282283 SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY ,
283284 SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY )) {
284285 pr_warn ("%.4s: read data[%d] fail\n" , key , i );
@@ -298,37 +299,80 @@ static int read_smc(struct applesmc_device *smc, u8 cmd, const char *key,
298299 if (i )
299300 pr_warn ("flushed %d bytes, last value is: %d\n" , i , data );
300301
301- return wait_status (smc , 0 , SMC_STATUS_BUSY );
302+ return port_wait_status (smc , 0 , SMC_STATUS_BUSY );
302303}
303304
304- static int write_smc (struct applesmc_device * smc , u8 cmd , const char * key ,
305+ static int port_write_smc (struct applesmc_device * smc , u8 cmd , const char * key ,
305306 const u8 * buffer , u8 len )
306307{
307308 int i ;
308309 int ret ;
309310
310- ret = smc_sane (smc );
311+ ret = port_smc_sane (smc );
311312 if (ret )
312313 return ret ;
313314
314- if (send_command (smc , cmd ) || send_argument (smc , key )) {
315+ if (port_send_command (smc , cmd ) || port_send_argument (smc , key )) {
315316 pr_warn ("%s: write arg fail\n" , key );
316317 return - EIO ;
317318 }
318319
319- if (send_byte (smc , len , APPLESMC_DATA_PORT )) {
320+ if (port_send_byte (smc , len , APPLESMC_DATA_PORT )) {
320321 pr_warn ("%.4s: write len fail\n" , key );
321322 return - EIO ;
322323 }
323324
324325 for (i = 0 ; i < len ; i ++ ) {
325- if (send_byte (smc , buffer [i ], APPLESMC_DATA_PORT )) {
326+ if (port_send_byte (smc , buffer [i ], APPLESMC_DATA_PORT )) {
326327 pr_warn ("%s: write data fail\n" , key );
327328 return - EIO ;
328329 }
329330 }
330331
331- return wait_status (smc , 0 , SMC_STATUS_BUSY );
332+ return port_wait_status (smc , 0 , SMC_STATUS_BUSY );
333+ }
334+
335+ static int port_get_smc_key_info (struct applesmc_device * smc ,
336+ const char * key , struct applesmc_entry * info )
337+ {
338+ int ret ;
339+ u8 raw [6 ];
340+
341+ ret = port_read_smc (smc , APPLESMC_GET_KEY_TYPE_CMD , key , raw , 6 );
342+ if (ret )
343+ return ret ;
344+ info -> len = raw [0 ];
345+ memcpy (info -> type , & raw [1 ], 4 );
346+ info -> flags = raw [5 ];
347+ return 0 ;
348+ }
349+
350+ static int read_smc (struct applesmc_device * smc , const char * key ,
351+ u8 * buffer , u8 len )
352+ {
353+ return port_read_smc (smc , APPLESMC_READ_CMD , key , buffer , len );
354+ }
355+
356+ static int write_smc (struct applesmc_device * smc , const char * key ,
357+ const u8 * buffer , u8 len )
358+ {
359+ return port_write_smc (smc , APPLESMC_WRITE_CMD , key , buffer , len );
360+ }
361+
362+ static int get_smc_key_by_index (struct applesmc_device * smc ,
363+ unsigned int index , char * key )
364+ {
365+ __be32 be ;
366+
367+ be = cpu_to_be32 (index );
368+ return port_read_smc (smc , APPLESMC_GET_KEY_BY_INDEX_CMD ,
369+ (const char * ) & be , (u8 * ) key , 4 );
370+ }
371+
372+ static int get_smc_key_info (struct applesmc_device * smc , const char * key ,
373+ struct applesmc_entry * info )
374+ {
375+ return port_get_smc_key_info (smc , key , info );
332376}
333377
334378static int read_register_count (struct applesmc_device * smc ,
@@ -337,8 +381,8 @@ static int read_register_count(struct applesmc_device *smc,
337381 __be32 be ;
338382 int ret ;
339383
340- ret = read_smc (smc , APPLESMC_READ_CMD , KEY_COUNT_KEY , (u8 * )& be , 4 );
341- if (ret )
384+ ret = read_smc (smc , KEY_COUNT_KEY , (u8 * )& be , 4 );
385+ if (ret < 0 )
342386 return ret ;
343387
344388 * count = be32_to_cpu (be );
@@ -360,7 +404,7 @@ static int applesmc_read_entry(struct applesmc_device *smc,
360404 if (entry -> len != len )
361405 return - EINVAL ;
362406 mutex_lock (& smc -> reg .mutex );
363- ret = read_smc (smc , APPLESMC_READ_CMD , entry -> key , buf , len );
407+ ret = read_smc (smc , entry -> key , buf , len );
364408 mutex_unlock (& smc -> reg .mutex );
365409
366410 return ret ;
@@ -374,7 +418,7 @@ static int applesmc_write_entry(struct applesmc_device *smc,
374418 if (entry -> len != len )
375419 return - EINVAL ;
376420 mutex_lock (& smc -> reg .mutex );
377- ret = write_smc (smc , APPLESMC_WRITE_CMD , entry -> key , buf , len );
421+ ret = write_smc (smc , entry -> key , buf , len );
378422 mutex_unlock (& smc -> reg .mutex );
379423 return ret ;
380424}
@@ -383,8 +427,7 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
383427 struct applesmc_device * smc , int index )
384428{
385429 struct applesmc_entry * cache = & smc -> reg .cache [index ];
386- u8 key [4 ], info [6 ];
387- __be32 be ;
430+ char key [4 ];
388431 int ret = 0 ;
389432
390433 if (cache -> valid )
@@ -394,18 +437,14 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(
394437
395438 if (cache -> valid )
396439 goto out ;
397- be = cpu_to_be32 (index );
398- ret = read_smc (smc , APPLESMC_GET_KEY_BY_INDEX_CMD , (u8 * )& be , key , 4 );
440+ ret = get_smc_key_by_index (smc , index , key );
399441 if (ret )
400442 goto out ;
401- ret = read_smc (smc , APPLESMC_GET_KEY_TYPE_CMD , key , info , 6 );
443+ memcpy (cache -> key , key , 4 );
444+
445+ ret = get_smc_key_info (smc , key , cache );
402446 if (ret )
403447 goto out ;
404-
405- memcpy (cache -> key , key , 4 );
406- cache -> len = info [0 ];
407- memcpy (cache -> type , & info [1 ], 4 );
408- cache -> flags = info [5 ];
409448 cache -> valid = true;
410449
411450out :
0 commit comments