3030 * authorization from the copyright holder(s) and author(s).
3131 */
3232
33+ #include <linux/ctype.h>
3334#include <linux/list.h>
3435#include <linux/list_sort.h>
3536#include <linux/export.h>
@@ -1405,6 +1406,151 @@ void drm_connector_list_update(struct drm_connector *connector)
14051406}
14061407EXPORT_SYMBOL (drm_connector_list_update );
14071408
1409+ static int drm_mode_parse_cmdline_bpp (const char * str , char * * end_ptr ,
1410+ struct drm_cmdline_mode * mode )
1411+ {
1412+ unsigned int bpp ;
1413+
1414+ if (str [0 ] != '-' )
1415+ return - EINVAL ;
1416+
1417+ str ++ ;
1418+ bpp = simple_strtol (str , end_ptr , 10 );
1419+ if (* end_ptr == str )
1420+ return - EINVAL ;
1421+
1422+ mode -> bpp = bpp ;
1423+ mode -> bpp_specified = true;
1424+
1425+ return 0 ;
1426+ }
1427+
1428+ static int drm_mode_parse_cmdline_refresh (const char * str , char * * end_ptr ,
1429+ struct drm_cmdline_mode * mode )
1430+ {
1431+ unsigned int refresh ;
1432+
1433+ if (str [0 ] != '@' )
1434+ return - EINVAL ;
1435+
1436+ str ++ ;
1437+ refresh = simple_strtol (str , end_ptr , 10 );
1438+ if (* end_ptr == str )
1439+ return - EINVAL ;
1440+
1441+ mode -> refresh = refresh ;
1442+ mode -> refresh_specified = true;
1443+
1444+ return 0 ;
1445+ }
1446+
1447+ static int drm_mode_parse_cmdline_extra (const char * str , int length ,
1448+ struct drm_connector * connector ,
1449+ struct drm_cmdline_mode * mode )
1450+ {
1451+ int i ;
1452+
1453+ for (i = 0 ; i < length ; i ++ ) {
1454+ switch (str [i ]) {
1455+ case 'i' :
1456+ mode -> interlace = true;
1457+ break ;
1458+ case 'm' :
1459+ mode -> margins = true;
1460+ break ;
1461+ case 'D' :
1462+ if (mode -> force != DRM_FORCE_UNSPECIFIED )
1463+ return - EINVAL ;
1464+
1465+ if ((connector -> connector_type != DRM_MODE_CONNECTOR_DVII ) &&
1466+ (connector -> connector_type != DRM_MODE_CONNECTOR_HDMIB ))
1467+ mode -> force = DRM_FORCE_ON ;
1468+ else
1469+ mode -> force = DRM_FORCE_ON_DIGITAL ;
1470+ break ;
1471+ case 'd' :
1472+ if (mode -> force != DRM_FORCE_UNSPECIFIED )
1473+ return - EINVAL ;
1474+
1475+ mode -> force = DRM_FORCE_OFF ;
1476+ break ;
1477+ case 'e' :
1478+ if (mode -> force != DRM_FORCE_UNSPECIFIED )
1479+ return - EINVAL ;
1480+
1481+ mode -> force = DRM_FORCE_ON ;
1482+ break ;
1483+ default :
1484+ return - EINVAL ;
1485+ }
1486+ }
1487+
1488+ return 0 ;
1489+ }
1490+
1491+ static int drm_mode_parse_cmdline_res_mode (const char * str , unsigned int length ,
1492+ bool extras ,
1493+ struct drm_connector * connector ,
1494+ struct drm_cmdline_mode * mode )
1495+ {
1496+ const char * str_start = str ;
1497+ bool rb = false, cvt = false;
1498+ int xres = 0 , yres = 0 ;
1499+ int remaining , i ;
1500+ char * end_ptr ;
1501+
1502+ xres = simple_strtol (str , & end_ptr , 10 );
1503+ if (end_ptr == str )
1504+ return - EINVAL ;
1505+
1506+ if (end_ptr [0 ] != 'x' )
1507+ return - EINVAL ;
1508+ end_ptr ++ ;
1509+
1510+ str = end_ptr ;
1511+ yres = simple_strtol (str , & end_ptr , 10 );
1512+ if (end_ptr == str )
1513+ return - EINVAL ;
1514+
1515+ remaining = length - (end_ptr - str_start );
1516+ if (remaining < 0 )
1517+ return - EINVAL ;
1518+
1519+ for (i = 0 ; i < remaining ; i ++ ) {
1520+ switch (end_ptr [i ]) {
1521+ case 'M' :
1522+ cvt = true;
1523+ break ;
1524+ case 'R' :
1525+ rb = true;
1526+ break ;
1527+ default :
1528+ /*
1529+ * Try to pass that to our extras parsing
1530+ * function to handle the case where the
1531+ * extras are directly after the resolution
1532+ */
1533+ if (extras ) {
1534+ int ret = drm_mode_parse_cmdline_extra (end_ptr + i ,
1535+ 1 ,
1536+ connector ,
1537+ mode );
1538+ if (ret )
1539+ return ret ;
1540+ } else {
1541+ return - EINVAL ;
1542+ }
1543+ }
1544+ }
1545+
1546+ mode -> xres = xres ;
1547+ mode -> yres = yres ;
1548+ mode -> cvt = cvt ;
1549+ mode -> rb = rb ;
1550+
1551+ return 0 ;
1552+ }
1553+
14081554/**
14091555 * drm_mode_parse_command_line_for_connector - parse command line modeline for connector
14101556 * @mode_option: optional per connector mode option
@@ -1431,13 +1577,12 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
14311577 struct drm_cmdline_mode * mode )
14321578{
14331579 const char * name ;
1434- unsigned int namelen ;
1435- bool res_specified = false, bpp_specified = false, refresh_specified = false;
1436- unsigned int xres = 0 , yres = 0 , bpp = 32 , refresh = 0 ;
1437- bool yres_specified = false, cvt = false, rb = false;
1438- bool interlace = false, margins = false, was_digit = false;
1439- int i ;
1440- enum drm_connector_force force = DRM_FORCE_UNSPECIFIED ;
1580+ bool parse_extras = false;
1581+ unsigned int bpp_off = 0 , refresh_off = 0 ;
1582+ unsigned int mode_end = 0 ;
1583+ char * bpp_ptr = NULL , * refresh_ptr = NULL , * extra_ptr = NULL ;
1584+ char * bpp_end_ptr = NULL , * refresh_end_ptr = NULL ;
1585+ int ret ;
14411586
14421587#ifdef CONFIG_FB
14431588 if (!mode_option )
@@ -1450,127 +1595,77 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
14501595 }
14511596
14521597 name = mode_option ;
1453- namelen = strlen (name );
1454- for (i = namelen - 1 ; i >= 0 ; i -- ) {
1455- switch (name [i ]) {
1456- case '@' :
1457- if (!refresh_specified && !bpp_specified &&
1458- !yres_specified && !cvt && !rb && was_digit ) {
1459- refresh = simple_strtol (& name [i + 1 ], NULL , 10 );
1460- refresh_specified = true;
1461- was_digit = false;
1462- } else
1463- goto done ;
1464- break ;
1465- case '-' :
1466- if (!bpp_specified && !yres_specified && !cvt &&
1467- !rb && was_digit ) {
1468- bpp = simple_strtol (& name [i + 1 ], NULL , 10 );
1469- bpp_specified = true;
1470- was_digit = false;
1471- } else
1472- goto done ;
1473- break ;
1474- case 'x' :
1475- if (!yres_specified && was_digit ) {
1476- yres = simple_strtol (& name [i + 1 ], NULL , 10 );
1477- yres_specified = true;
1478- was_digit = false;
1479- } else
1480- goto done ;
1481- break ;
1482- case '0' ... '9' :
1483- was_digit = true;
1484- break ;
1485- case 'M' :
1486- if (yres_specified || cvt || was_digit )
1487- goto done ;
1488- cvt = true;
1489- break ;
1490- case 'R' :
1491- if (yres_specified || cvt || rb || was_digit )
1492- goto done ;
1493- rb = true;
1494- break ;
1495- case 'm' :
1496- if (cvt || yres_specified || was_digit )
1497- goto done ;
1498- margins = true;
1499- break ;
1500- case 'i' :
1501- if (cvt || yres_specified || was_digit )
1502- goto done ;
1503- interlace = true;
1504- break ;
1505- case 'e' :
1506- if (yres_specified || bpp_specified || refresh_specified ||
1507- was_digit || (force != DRM_FORCE_UNSPECIFIED ))
1508- goto done ;
15091598
1510- force = DRM_FORCE_ON ;
1511- break ;
1512- case 'D' :
1513- if (yres_specified || bpp_specified || refresh_specified ||
1514- was_digit || (force != DRM_FORCE_UNSPECIFIED ))
1515- goto done ;
1599+ if (!isdigit (name [0 ]))
1600+ return false;
15161601
1517- if ((connector -> connector_type != DRM_MODE_CONNECTOR_DVII ) &&
1518- (connector -> connector_type != DRM_MODE_CONNECTOR_HDMIB ))
1519- force = DRM_FORCE_ON ;
1520- else
1521- force = DRM_FORCE_ON_DIGITAL ;
1522- break ;
1523- case 'd' :
1524- if (yres_specified || bpp_specified || refresh_specified ||
1525- was_digit || (force != DRM_FORCE_UNSPECIFIED ))
1526- goto done ;
1602+ /* Try to locate the bpp and refresh specifiers, if any */
1603+ bpp_ptr = strchr (name , '-' );
1604+ if (bpp_ptr ) {
1605+ bpp_off = bpp_ptr - name ;
1606+ mode -> bpp_specified = true;
1607+ }
15271608
1528- force = DRM_FORCE_OFF ;
1529- break ;
1530- default :
1531- goto done ;
1532- }
1609+ refresh_ptr = strchr (name , '@' );
1610+ if (refresh_ptr ) {
1611+ refresh_off = refresh_ptr - name ;
1612+ mode -> refresh_specified = true;
15331613 }
15341614
1535- if ( i < 0 && yres_specified ) {
1536- char * ch ;
1537- xres = simple_strtol ( name , & ch , 10 );
1538- if (( ch != NULL ) && ( * ch == 'x' ))
1539- res_specified = true ;
1540- else
1541- i = ch - name ;
1542- } else if (! yres_specified && was_digit ) {
1543- /* catch mode that begins with digits but has no 'x' */
1544- i = 0 ;
1615+ /* Locate the end of the name / resolution, and parse it */
1616+ if ( bpp_ptr && refresh_ptr ) {
1617+ mode_end = min ( bpp_off , refresh_off );
1618+ } else if ( bpp_ptr ) {
1619+ mode_end = bpp_off ;
1620+ } else if ( refresh_ptr ) {
1621+ mode_end = refresh_off ;
1622+ } else {
1623+ mode_end = strlen ( name );
1624+ parse_extras = true ;
15451625 }
1546- done :
1547- if (i >= 0 ) {
1548- pr_warn ("[drm] parse error at position %i in video mode '%s'\n" ,
1549- i , name );
1550- mode -> specified = false;
1626+
1627+ ret = drm_mode_parse_cmdline_res_mode (name , mode_end ,
1628+ parse_extras ,
1629+ connector ,
1630+ mode );
1631+ if (ret )
15511632 return false;
1552- }
1633+ mode -> specified = true;
15531634
1554- if (res_specified ) {
1555- mode -> specified = true ;
1556- mode -> xres = xres ;
1557- mode -> yres = yres ;
1635+ if (bpp_ptr ) {
1636+ ret = drm_mode_parse_cmdline_bpp ( bpp_ptr , & bpp_end_ptr , mode ) ;
1637+ if ( ret )
1638+ return false ;
15581639 }
15591640
1560- if (refresh_specified ) {
1561- mode -> refresh_specified = true;
1562- mode -> refresh = refresh ;
1641+ if (refresh_ptr ) {
1642+ ret = drm_mode_parse_cmdline_refresh (refresh_ptr ,
1643+ & refresh_end_ptr , mode );
1644+ if (ret )
1645+ return false;
15631646 }
15641647
1565- if (bpp_specified ) {
1566- mode -> bpp_specified = true;
1567- mode -> bpp = bpp ;
1648+ /*
1649+ * Locate the end of the bpp / refresh, and parse the extras
1650+ * if relevant
1651+ */
1652+ if (bpp_ptr && refresh_ptr )
1653+ extra_ptr = max (bpp_end_ptr , refresh_end_ptr );
1654+ else if (bpp_ptr )
1655+ extra_ptr = bpp_end_ptr ;
1656+ else if (refresh_ptr )
1657+ extra_ptr = refresh_end_ptr ;
1658+
1659+ if (extra_ptr ) {
1660+ int remaining = strlen (name ) - (extra_ptr - name );
1661+
1662+ /*
1663+ * We still have characters to process, while
1664+ * we shouldn't have any
1665+ */
1666+ if (remaining > 0 )
1667+ return false;
15681668 }
1569- mode -> rb = rb ;
1570- mode -> cvt = cvt ;
1571- mode -> interlace = interlace ;
1572- mode -> margins = margins ;
1573- mode -> force = force ;
15741669
15751670 return true;
15761671}
0 commit comments